您的位置:首页 > 其它

1001. A+B Format (20)

2016-04-05 08:42 239 查看


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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: