PAT 1001. A+B Format (20)

1001. A+B Format (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

思路:

注意查看题目的意思,一个数分为三组,当然大于等于1e6一组(注意等号),介于小于1e6并且大于等于1e3的是一组(注意等号),小于1e3的是一组。

中间数值的输出注意用03d,这个举例,比如:1,000

代码:

#include <bits/stdc++.h>
using namespace std;
const int moda = 1e6;
const int modb = 1e3;
int main() {
	int a,b,sum;
	scanf("%d %d",&a,&b);
	sum=a+b;
	if(sum<0) {
		printf("-");
		sum=-sum;
	}
	if(sum>=moda) {
		printf("%d,%03d,%03d
", sum/moda,(sum%moda)/modb,sum%modb);
	} else if(sum>=modb) {
		printf("%d,%03d
", sum/modb,sum%modb);
	} else {
		printf("%d
", sum);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/lemonbiscuit/p/7775970.html