PAT 甲级1001 求助

qq_36317016 2018-01-22 08:34:51
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个报错,实在想不到了,想问问大家我是漏了什么边界条件么。。
...全文
386 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36317016 2018-01-22
  • 打赏
  • 举报
回复
已经解决,答案是格式控制不对,如果*iter为0,则输出的是1个0,不是3个0 。

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧