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 −1e6≤a,b≤1e6.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
题目大意就是求两个给定范围的数相加,再用标准形式输出(每三位之间有‘,’隔开)
输出主要考虑三部分即可:符号,输出位数,逗号;
符号其实挺简单的,只要判断sum的正负即可;
输出时:
1)sum<1000时,直接输出即可,不用考虑逗号问题;
2)sum>1000时,在这里我利用了数组来储存sum的每个位的数字,然后按照每输出一位,就要判断剩余位数是否是3的倍数,如果是,就要输出逗号;
(我一开始考虑的是从前向后输出,先输出前三位,然后加‘,’后再输出后三位,但这样显然不适用于6位以上,如1000000+7,正确输出:1,000,007;如果按我的想法就会变成1000,007;如果要更正,就要将前三位输出再进行分段,远不如直接用数组效率高)
代码: