uTank-木头
每一个你不满意的现在,都有一个你没有努力的曾经。

【题目链接】

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

题意分析:

输出(a+b)的结果,输出格式需要使用逗号把数值分割。

提交代码:

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int out[10];
 6     int i, len, tmp;
 7     int a, b, c;
 8 
 9     scanf("%d %d", &a, &b);
10 
11     c = a + b;
12     if(c < 0)
13     {
14         printf("-");
15         c = -c;
16     }
17 
18     len = 0;
19     do {
20         out[len] = c % 10;
21         c /= 10;
22         len++;
23     } while(c != 0);
24 
25     for(i = 0; i < len; i++)
26     {
27         printf("%d", out[len-1-i]);
28         tmp = len - i - 1;
29         //if((len-i-1)%3 == 0 && (len-i) > 1)
30         if(tmp != 0 && tmp % 3 == 0)
31             printf(",");
32     }
33 
34     return 0;
35 }

 

posted on 2015-09-01 14:40  uTank  阅读(211)  评论(0编辑  收藏  举报