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

最近开始做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).

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
#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;
}

第一个题还是比较容易的,注意数组初始化,不然要出事。

猜你喜欢

转载自blog.csdn.net/coderwait/article/details/79645268
今日推荐