1001 A+B Format (20分) Python解法部分正确

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 −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 a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

#1001 A+B Format (20分).py
a, b = map(int, input().split())
c = a+b
flag = 0
if c<0:#负数变为整数,统一处理
    flag = 1
    c = -c
c = list(str(c))
if len(c)>=4:
    for i in range(len(c)):
        if i%3 == 0 and i != 0:#找到每三位数的位置
            c[i-1] = str(c[i-1])+','#添加一个逗号
if flag == 1:
    c.insert(0, '-')
print(''.join(c))

1001 A+B Format (20分) Python解法部分正确_第1张图片
人间迷惑行为…验证了几个数字还可以用,求路过大神指点,跪谢!!

你可能感兴趣的:(PAT)