1001. A+B Format (20)

来源:互联网 发布:魔兽争霸3 原生 mac 编辑:程序博客网 时间:2024/05/16 17:22

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three bycommas (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 <vector>using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char** argv) {int a,b;int res;bool flag=false;vector<char> v;scanf("%d %d",&a,&b);res=a+b;if(res==0){printf("0");return 0;}if(res<0){res=(-1)*res;flag=true;}int s=0;while(res>0){char tmp=res%10+'0';v.push_back(tmp);res/=10;s++;if(s==3 && res>0){s=0;v.push_back(',');}}for(int i=v.size()-1; i>=0; i--){if(flag){flag=false;printf("-");}printf("%c",v[i]);}return 0;}



0 0