【PAT】A1001. A+B Format (20)

Description:
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
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output
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

//NKW 甲级练习题1013
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <stack>
using namespace std;
const int maxn = 20;
stack<char> st;                   //练一下stack
int main(){
	int a, b, cnt = 0;
	bool flag = false;
	scanf("%d %d", &a, &b);
	a = a + b;
	if (a < 0)
		printf("-");
	while(a != 0){
		st.push(abs(a % 10));		//负数的模是负数
		a = a / 10;
		cnt++;
	}
	//printf("cnt=%d", cnt);
	for (int i = cnt; !st.empty();i--){
		if (flag == true && i % 3 == 0)		//第一位不能是‘,’
			printf(",");
		printf("%d", st.top());
		flag = true;
		st.pop();
	}
	printf("\n");
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ztmajor/article/details/80932281
今日推荐