PAT 甲级1001 求助
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个报错,实在想不到了,想问问大家我是漏了什么边界条件么。。
时间限制
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个报错,实在想不到了,想问问大家我是漏了什么边界条件么。。
...全文
相关推荐
浙大PAT全套答案_1001至1049.
浙大PAT全套答案_1001至1049.
PAT甲级(1001-1115)题库
这是PAT甲级第1001到1115的所有原题,只有题目没有答案。
PAT:PAT甲级练习
拍 PAT甲级练习,大多都是自己完成,有些没AC的看了网上的测试点然后自己改的
【C++】PAT甲级题目实现
为了记录我在准备PAT甲级的练习,特开此页。 在练习的过程中吸收了大量博主的经验,特别是大神柳婼:https://blog.csdn.net/liuchuo。 但是由于代码风格的不同,我在学习他们的想法的同时也在努力形成自己的C++代码风格。 题号 名字 链接 1004 Counting Leaves https://blog.csdn.net/weixin_42426496/article/details/104222455 1006 Sign In and Sign Out https://blog.csdn.net/weixin_42426496/article/detai
PAT甲级复习/CAIP复习
骗分思路之查数据范围.pdf 算法笔记.胡凡.pdf 算法考前必看汇总.pdf(只看考点就行) 力扣题库.md(只看考点就行)


其它技术问题
