1001 A+B Format (20)(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
#include<iostream> #include<cstdio> #include<cstring> #include<stdlib.h> #include<algorithm> using namespace std; char s[30]; int main() { int a,b; cin>>a>>b; a=a+b; int start=0; int temp,len=0; b=a; if(a<0)a=-a; if(a==0)cout<<0; while(a){ temp=a%10; a/=10; len++; if(len>3&&len%3==1) s[start++]=','; s[start++]=temp+'0'; } if(b<0) cout<<'-'; for(int i=start-1;i>=0;i--) cout<<s[i]; return 0; }
//就是求a+b的和,并且输出。非常简单。并且和都不会溢出int.第一次提交的时候只得了19分,发现是因为在输入两个0时,程序没有输出。之后对两者和进行判断,若和为0,直接输出0即可。