1001. A+B Format (20)

原创 2015年07月09日 14:35:10

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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

PAT-A 1001. A+B Format (20)

PAT-A 1001. A+B Format (20) 逗号输出有坑……

PAT1001. A+B Format(20)

最近开始每天做一道PAT练手,下面是PAT1001的题目。 Calculate a + b and output the sum in standard format -- that is, th...
  • wtrnash
  • wtrnash
  • 2017年03月27日 20:41
  • 110

[PAT]1001. A+B Format (20)

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...
  • fnzsjt
  • fnzsjt
  • 2014年07月21日 15:26
  • 518

PAT 1001. A+B Format (20)(字符串转换)

题目1001. A+B Format (20)时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate...

1001. A+B Format (20) C++

问题描述: Calculate a + b and output the sum in standard format – that is, the digits must be separated ...

1001. A+B Format (20)

1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B ...
  • fynjy
  • fynjy
  • 2015年07月06日 13:35
  • 231

PAT:A1001. A+B Format (20/20)

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...

Pat 1001. A+B format

此题为两个数求和后进行格式化输出,因为没有超出Integer的表示范围,可以直接整数求和。 思路如下:import java.io.BufferedReader; import java.io.IOE...

PAT_1001: A+B Format

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...

1001. A+B Format

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...
  • chr1991
  • chr1991
  • 2016年03月09日 21:10
  • 225
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:1001. A+B Format (20)
举报原因:
原因补充:

(最多只允许输入30个字)