标签:
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 }
标签:
原文地址:http://www.cnblogs.com/GoFly/p/4242256.html