1001. A+B Format (20) (%0nd)

 

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

 

解题思路:这里主要是注意一下补0的问题

     %nd:n代表的是列宽长度。

 

     (1)%-nd   -  代表的是左对齐。

     (2)%nd       代表的是右对齐。

 

     (3)%0nd   0(数字零)代表的是不足n位长度的左补齐0。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     int a,b;
 7     int sum = 0;
 8     scanf("%d %d",&a,&b);
 9 
10     sum = a+b;
11     if( sum<0){
12         printf("-");
13         sum = 0-sum;
14     }
15     if( sum>=1000000){
16         printf("%d,%03d,%03d",(sum/1000000),(sum/1000%1000),(sum%1000));
17     }
18     else if( sum>=1000){
19         printf("%d,%03d",(sum/1000),(sum%1000));
20     }
21     else printf("%d",sum);
22     return 0;
23 }

 

posted @ 2018-02-06 10:38  yuxiaoba  阅读(315)  评论(0编辑  收藏  举报