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
使用一个stack作辅助,每次压入低三位,sum=sum/1000
/*2015.7.19cyq*/ #include <iostream> #include <stack> using namespace std; int main(){ int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum==0){ printf("0"); return 0; } if(sum<0){ printf("-"); sum=-sum; } stack<int> rt; while(sum){ rt.push(sum%1000); sum/=1000; } bool firstTime=true; while(!rt.empty()){ int k=rt.top(); rt.pop(); if(firstTime){ printf("%d",k);//只有第一次不用补零 firstTime=false; }else printf("%03d",k); if(!rt.empty()) printf(","); } return 0; }