标签:
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 9Sample Output
-999,991
主要是题目的groups of three 的of 的用法让我有点迷糊,最后确认是一种属性关系,在题目的意思为3个一组。
代码为:
1 /* 2 https://www.patest.cn/contests/pat-a-practise/1001 3 author: LY 2016.6.26 4 */ 5 #include<iostream> 6 #include<string> 7 #include<algorithm> 8 using namespace std; 9 10 int main() 11 { 12 int a,b,c; 13 string s; 14 scanf("%d%d",&a,&b); 15 16 c=a+b; 17 int i=0,l=0; 18 int flag=0; //符号判断 19 if(c<0) 20 { 21 flag=1; 22 c=abs(c); 23 } 24 while((c/10)!=0) 25 { 26 s[i]=(c%10)+‘0‘; 27 c=c/10; 28 i++; 29 l++; 30 if(l%3==0) 31 { 32 s[i]=‘,‘; 33 i++; 34 } 35 } 36 s[i]= c+‘0‘; 37 38 //对a+b的和进行拆分(每3个数一组) 39 int count=1; 40 if(flag==1) 41 printf("%c",‘-‘); 42 for(int j=i;j>=0;j--) 43 { 44 printf("%c",s[j]); 45 } 46 47 printf("\n"); 48 return 0; 49 }
标签:
原文地址:http://www.cnblogs.com/ly199553/p/5536195.html