码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1001. A+B Format (20)

时间:2015-01-22 19:44:39      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:

1001. A+B Format (20)

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

思路:每三个为一组,进行取值,然后调整输出格式
技术分享
 1 #include<stdio.h>
 2 
 3 int main(int argc, char *argv[])
 4 {
 5     int a,b;
 6     scanf("%d%d",&a,&b);
 7     int sum=a+b;
 8     int first,second,third;//以3个为一组 
 9        first=sum/1000000;
10        if(first!=0)
11        {
12            printf("%d,",first);
13     }
14     second=sum/1000%1000;
15     if(first!=0)
16     {
17         if(second<0)
18            second=-second;
19         printf("%03d,",second);
20     }
21     else if(first==0&&second!=0)
22     {
23        printf("%d,",second);    
24     }
25     third=sum%1000;
26     if(first==0&&second==0)
27        printf("%d\n",third);
28     else if(first!=0||(first==0&&second!=0))
29     {
30         if(third<0)
31            third=-third;
32         printf("%03d\n",third);
33     }
34       return 0;
35 }
View Code

 

PAT1001. A+B Format (20)

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4242256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!