【题解】PATA-1001 A+B Format

A+B Format(PATA-1001)

题面

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).

输入

Each input file contains one test case. Each case contains a pair of integers a and b where −10^6≤a,b≤10^6. The numbers are separated by a space.

输出

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.

样例输入

1
-1000000 9

样例输出

1
-999,991

提示

思路

代码

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
int a, b; scanf("%d %d", &a, &b);
char s[20]; sprintf(s, "%d", a+b);
int len=strlen(s), f=(a+b<0), st, num;
if(f){
st = (len-2)%3+1;
num = (len-2)/3;
}else{
st = (len-1)%3;
num = (len-1)/3;
}
for(int i=0; i<len; i++){
printf("%c", s[i]);
if(num && i==st){
printf(",");
num--, st+=3;
}
}
return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/