1001. A+B Format (20)

1.主要是字符串处理,然后插入逗号

2.注意正负数

3.输入可以直接使用int类型读取

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

 

AC代码

//#include<string>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include <iomanip>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;

int main(void)
{
	int a, b;
	cin >> a >> b;
	int result = a + b;
	bool sign = true;//正负符号标志位
	if (result < 0)
	{
		result = -result;
		sign = false;
	}
	string str = "";
	if (result == 0) str = "0";
	while (result != 0)
	{//转化成string
		char c = result % 10 + '0';
		str += c;
		result /= 10;
	}

	//此时的str是倒序的
	string output = "";
	for (int i = 0; i < str.size() - 1; i++)
	{
		output = str[i] + output;
		if ((i+1) % 3 == 0)
			output = ',' + output;
	}
	output = str[str.size() - 1] + output;
	if (!sign) output = "-" + output;
	cout << output << endl;

	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注