1001. A+B Format (20)

1001. A+B Format (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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>
using namespace std;
int main()
{
	long long int a=0,b=0,c=0;
	int temp=0,num=0,flag=0;
	vector<char> vec1;
	vector<char>::iterator iter;
	cin>>a>>b;
	c=a+b;
	if(c<0)//标志位flag,当为负数的时候,标志位置1
	{
		flag=1;
		c=c*(-1);
	}
	if(c>=1000)//大于四位数时候
	{
		if(c>=100000&&c<=999999)//特殊考虑六位数,直接将逗号插入
		{
			while(c)
			{
				temp=c%10;
				vec1.push_back(temp+48);
				c=c/10;
			}
			vec1.insert(vec1.begin()+3,',');
		}
		else//其他情况按照三个一组插入逗号,其实也可以不分开考虑,直接考虑vector最后一个元素是否为‘,’如果是逗号,弹出
		{
			while(c)
			{
				temp=c%10;
				vec1.push_back(temp+48);
				num++;
				c=c/10;
				if(num==3)
				{
					vec1.push_back(',');
					num=0;
				}
			}
		}
		if(flag==1)
			cout<<'-';
		for(int i=vec1.size()-1;i>=0;i--)
			cout<<vec1.at(i);
	}
	else//三位数以下时候直接输出,注意为负数时候要输出负号
	{
		if(flag==1)
			cout<<'-';
		cout<<c;
	}
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值