关闭

1001. A+B Format (20)

227人阅读 评论(0) 收藏 举报
分类:

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>
#include<vector>
#include<stack>

using namespace std;
int  main(){
	stack<char> v;
	int a,b;
	cin>>a>>b;
	int c=a+b;
	int sign=0;
	if(c==0){
		cout<<"0"<<endl;
		return 0;
	}
	if(c<0){
		sign=1;
		c=0-c;
	}
	int count=0;
	int flag=0;
	while(c>0){
		int temp=c%10;
		count++;
		v.push(temp+48);
		if(count%3==0 && count>0){
			flag++;
		    v.push(44);
		}
		c=c/10;
	}
	if((v.size()-flag)%3==0)
		v.pop();
	if(sign==1){
		cout<<'-';
	}
	while(!v.empty()){	
		cout<<v.top();
		v.pop();		
	}
	system("pause");
	return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:67786次
    • 积分:1559
    • 等级:
    • 排名:千里之外
    • 原创:75篇
    • 转载:100篇
    • 译文:0篇
    • 评论:0条