A1001 A+B Format [字符串处理]

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 Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106​​ . The numbers are separated by a space.

Output Specification:
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=0的时候while不会进行循环,要不就也可以
改成dowhile,不然就要加if,前面一直以为,是前三位打一个
后面才知道从后卫前三位一个逗号。
	if (c == 0) z[num++] = 0;
	while (c != 0)
	{
		z[num++] = c % 10;
		c /= 10;
	}
#include<iostream>
using namespace std;
int main()
{
	int a, b, c = 0;
	int z[20] = { 0 }; int num = 0;
	int flag = 0;
	cin >> a >> b;
	c = a + b;
	if (c < 0)
	{
		c = -c;
		flag = 1;
	}
	if (c == 0) z[num++] = 0;
	while (c != 0)
	{
		z[num++] = c % 10;
		c /= 10;
	}
	if (flag)
	{
		cout << '-';
	}
	int i = num-1;
	while (i!=-1)
	{
		cout << z[i];
		if (i%3==0&&i>0)
		{
			cout << ',';
		}
		i--;
	}
	

}
posted @ 2020-06-29 12:08  _Hsiung  阅读(70)  评论(0编辑  收藏  举报