A+B Format (20)

来源:互联网 发布:2016年上证指数数据 编辑:程序博客网 时间:2024/04/25 14:28

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>using namespace std;int main(){int N,M;while(scanf("%d%d",&N,&M)!=EOF){   int total=N+M;   bool isminus=false;   if(total<0)   {      total=(-1)*total;  isminus=true;   }          int parts[5];   int len=0;   if(total<1000)   {     parts[len]=total; total=0; len++;   }   while(total!=0)   {   parts[len]=total%1000;   total=total/1000;       len++;   }       len--;   if(isminus)   cout<<"-";   if(len!=0)   cout<<parts[len]<<",";   else cout<<parts[len]<<endl;   for(int i=len-1;i>0;i--)   {   if(parts[i]<10)         cout<<"00"<<parts[i]<<",";   else if(parts[i]<100)                cout<<"0"<<parts[i]<<",";   else                 cout<<parts[i]<<",";   }   if(len!=0)   { if(parts[0]<10)         cout<<"00"<<parts[0]<<endl;   else if(parts[0]<100)                cout<<"0"<<parts[0]<<endl;   else                 cout<<parts[0]<<endl;   } }  return 0;}
原创粉丝点击