您的位置:首页 > 其它

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

2018-03-21 20:31 856 查看
最近开始做PAT,正儿八经地练习代码。程序员真的离不开写代码,更离不开看别人的代码。有时候AC了看看大神的代码,才知道自己的算法是多愚蠢。Github和CSDN的大神是真的强!一个人写代码也没啥意思,也决定学学大神写写刷题记录,增加点儿趣味,谁还没个想做大神的心呢?
以上算是新博客的见面礼,不废话了,滚去写代码!!

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).InputEach 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.OutputFor 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<cstdlib>
using namespace std;
void DisplaySum(int n)
{
int sum[8] = { 0 }; int i = 0;
if (0 == n)
cout << "0";
if (n < 0)
{
n = -n;
cout << "-";
}
while (n)
{
sum[i] = n % 10;
n /= 10;
i++;
}
for (int j = i-1; j >=0; j--)
{
if (0 == j % 3&&j)
cout << sum[j] << ",";
else
cout << sum[j];
}
}
int main(void)
{
int a, b;
cin >> a >> b;
DisplaySum(a + b);
system ("PAUSE");
return 0;
}
第一个题还是比较容易的,注意数组初始化,不然要出事。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: