您的位置:首页 > 编程语言 > C语言/C++

【c++】PAT (Advanced Level) 1001. A+B Format (20)

2014-02-21 19:01 369 查看



1001. A+B Format (20)

时间限制
400 ms

内存限制
32000 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


测试点某个 如果直接输出数字 像 0 就不会显示3个0,而是显示1个0;
//Artemis
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;

void print(int a){
cout<<","<<setfill('0')<<setw(3)<<a;
}

int main(){
int a,b;
while(cin>>a>>b){
int c;
c=a+b;
//cout<<c<<endl;
vector <int> cc;
while(c/1000!=0){//超过三位数
int m2;
m2=c%1000;
c=c/1000;
if(m2<0){
m2=-m2;
}
cc.insert(cc.begin(),m2);
}
cout<<c;
for_each(cc.begin(),cc.end(),print);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: