pat 1001 A+B Format (20)

1001. A+B Format (20)

时间限制  
400 ms
内存限制  
65536 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


分析:
 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<stack>
 7 using namespace std;
 8 int main(){
 9     int a,b;
10     cin>>a>>b;
11         a=a+b;
12         if(a<0){
13             cout<<'-';
14             a*=-1;
15         }
16         int i=0;
17         int c[10];
18         if(a<1000){
19             cout<<a<<endl;
20         }
21         else{
22             //cout<<a<<endl;
23             while(a){
24                 c[i++]=a%10;
25                 a=a/10;
26             }
27             i--;
28             //cout<<i<<endl;
29             while(i>=0){
30                 cout<<c[i--];
31                 if(i%3==2){//摆放位置要正确,第一个数字一定要输出
32                     cout<<',';
33                 }
34             }
35             cout<<endl;
36         }
37     return 0;
38 }

 

posted @ 2015-03-13 17:19  Deribs4  阅读(207)  评论(0编辑  收藏  举报