PAT 甲级1001 求助

qq_36317016 qq_36317016 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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

以上是该题目要求
以下是我的简单c++实现,40行不到。


#include<iostream>
#include<vector>
#include<algorithm>


using namespace std;


int main()
{
int a,b;
cin >> a >> b;
int sum = a+b;

vector<int> c;
if(sum == 0)
{
cout << 0 ;
return 0;
}
if (sum < 0)
{
cout <<"-";
sum = -sum;
}
int remainder;
while(sum != 0)
{
remainder = sum%1000;
c.insert(c.begin(),remainder);
sum = sum/1000;
}
vector<int>::iterator iter;
cout << *c.begin();
for(iter = c.begin()+1; iter != c.end();++iter)
{
cout << "," << *iter;
}
return 0;
}
最后20个测试点有5个报错,实在想不到了,想问问大家我是漏了什么边界条件么。。
...全文
评论 1
收藏 已收藏
分享
返回顶部
qq_36317016
其它技术问题 3,881 成员 9,044 内容