1001. A+B Format (20)
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
# 1. 倒序遍历,每三个数插入一个逗号
# 2. 特殊情况一:负数最高位'-',符号情况很好判断,索性拿掉负号,正负一起复用代码
# 3. 特殊情况二:数位为3的倍数,最高位判断
# 4. 头插法是一种比较方便的方法
a,b = input().split()
a = int(a)
b = int(b)
s = ""
sum = a+b
sum = str(sum)
if a+b < 0:
sum = sum[1:]
for i in range(len(sum)):
index = i+1
c = sum[-index]
s = c + s
if index % 3 == 0 and index != len(sum):
s = ',' + s
if a+b < 0:
s = '-' + s
print(s)