1001 A+B Format

Calculate a+ba + 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 aa and bb where 106a,b106−10^6≤a,b≤10^6. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of aa and bb in one line. The sum must be written in the standard format.

Sample Input:

1
-1000000 9

Sample Output:

1
-999,991

思路

  • 使用Python处理字符串,一下子就简单了很多,当然这题用C++的string类拼接也可以,并不麻烦。不过我还是建议使用Python,因为所以的点都能过。
  • 读取两个数,求它们的和,保存成字符串。
  • 然后从后向前遍历,每隔三个位置,加一个逗号。
  • 当再向前一个是负号的时候不用加逗号了。

代码

1
2
3
4
5
6
7
a = str(sum(list(map(int,input().split()))))
count = 0
for i in range(len(a)-1,0,-1):
count += 1
if count % 3 == 0 and a[i-1]!='-':
a = a[:i] + ',' + a[i:]
print(a)

太水了这题。