http://pat.zju.edu.cn/contests/pat-practise/1001

来源:互联网 发布:java.io jar包 编辑:程序博客网 时间:2024/04/30 21:02

1001. A+B Format (20)

时间限制
400 ms
内存限制
32000 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
[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstdlib>  
  4. #include<algorithm>  
  5. #include<memory.h>  
  6. #include<string>  
  7. using namespace std;  
  8.   
  9. int main(){  
  10.     //freopen("in.txt", "r", stdin);  
  11.       
  12.     int a, b;  
  13.     char s[10];  
  14.     int ans;  
  15.     while(cin>>a>>b){  
  16.         ans = a+b;  
  17.         if(ans<0){  
  18.             printf("-");  
  19.             ans = -ans;  
  20.         }  
  21.           
  22.         int k = 0;  
  23.         if(ans==0)  
  24.             s[k++]='0';  
  25.         while(ans!=0){  
  26.             s[k++] = ans%10 + '0';  
  27.             ans /= 10;  
  28.         }  
  29.         s[k] = '\0';  
  30.           
  31.         int len = strlen(s);  
  32.         for(int i=len-1;i>=0;i--){  
  33.             printf("%c", s[i]);  
  34.             if(i%3==0 && i!=0)  
  35.                 printf(",");  
  36.         }  
  37.         printf("\n");  
  38.     }  
  39.   
  40.   
  41.     //fclose(stdin);  
  42.   
  43. }