A+B Format(stack的使用)

本文介绍了一道编程题1001A+BFormat的解答思路,该题要求计算两个整数的和并以标准格式输出,即每三位用逗号分隔。通过将结果转化为字符串并使用栈实现逆序输出,每三位插入一个逗号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1001 A+B Format (20 分)

题目链接
Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991

题意:

计算两个数的和,并按每三位加一个“,”输出

注意:要从最后一位开始算起,这也是这道题的坑点之一

【分析】

只要明白栈的原理:先进后出,把结果值从最后一位开始入栈,这样出栈的话就可以每三位加一个‘,’输出,但是得注意负数的时候栈中会多出一个负号,得特判一下即可

#include<cstdio>
#include<cstring>
#include<string>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<cstdlib>
#define eps 1e-9
const double pi = acos(-1.0);
#define ll long long
#define inf 0x3fffffff
const int N = 1010;
using namespace std;

int main()
{
	ll a, b, sum;
	stack<char>ss;
	cin >> a >> b;
	int cnt = 0;
	sum = a + b;

	string str = to_string(sum);//把结果存为字符串

	//cout << "此时结果为:" << str << endl;

	for (int i = str.size()-1; i >=0; i--)//运用栈的原理,
	{
		ss.push(str[i]);
		cnt++;
		
		//cout << "每一次的i的值:" << i << endl;

		if (i == 0)ss.push(str[i]);

		else if (cnt % 3 == 0 && str[i - 1] >= '0'&&str[i - 1] <= '9')
			{
				ss.push(',');
				cnt = 0;
			}
	}
	
	int f = 0;

	while (!ss.empty())
	{
		if (f == 0)ss.pop();//如果是负数的话,会多出一个“-”,所以要去掉第一个负号
		f = 1;
		cout << ss.top();
		ss.pop();
	}
	
	return 0;
}

你可以使用C语言编写这样一个简单的算法,步骤如下: 首先,我们需要创建两个数据结构,一个是顺序栈(数组实现),用于存储序列1;另一个是循环队列(队列元素动态分配),用于存储序列2。 1. 定义函数`isPalindromeAndConcatenation(char* input)`来进行主处理: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_LEN 100 // 队列最大长度 typedef struct { char data[MAX_LEN]; int front, rear; // 循环队列的front和rear指针 } Queue; // 初始化循环队列 Queue createQueue() { Queue q; q.front = q.rear = -1; return q; } // 入队操作 void enqueue(Queue *q, char c) { if (q->rear + 1 == MAX_LEN) { printf("Error: Queue overflow.\n"); exit(1); } q->data[q->rear + 1] = c; q->rear = (q->rear + 1) % MAX_LEN; } // 出队操作 char dequeue(Queue *q) { if (q->front == q->rear) { printf("Error: Queue underflow.\n"); exit(1); } char c = q->data[q->front]; q->front = (q->front + 1) % MAX_LEN; return c; } bool isPalindromeAndConcatenation(char* input) { Stack stack; // 顺序栈 Queue queue; stack.top = -1; queue = createQueue(); while (*input != '@') { // 遍历输入直到遇到@ if (*input == '&') { if (stack.top >= 0) { printf("Error: Found '&' in sequence1 or sequence2.\n"); return false; } continue; } // 将序列1推入栈,序列2入队 if (stack.top == -1) { stack.top++; stack.data[stack.top] = *input; } else { enqueue(&queue, *input); } input++; } // 检查序列2是否是序列1的逆序 if (stack.top > 0 && queue.front != -1) { while (queue.front != queue.rear && stack.top > 0) { if (queue.data[queue.front] != stack.data[stack.top]) { return false; } stack.top--; queue.front = (queue.front + 1) % MAX_LEN; } } else { return false; } // 如果到这一步还没返回false,说明满足条件 return true; } int main() { char input[] = "a+b&b+a"; if (isPalindromeAndConcatenation(input)) { printf("The input string is of the specified format.\n"); } else { printf("The input string does not match the format.\n"); } return 0; } ``` 这个程序首先检查输入中是否有'&',然后将字符分组入栈和队列。最后比较两部分是否是逆序,如果全部符合条件,则返回true,否则返回false。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值