PAT-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 −. 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

 

模拟一下,每三位加一个逗号

 1 a,b=input().split()
 2 a,b=int(a),int(b)
 3 res=a+b
 4 res=str(res)
 5 res=list(res)
 6 lens=len(res)
 7 j=0
 8 for i in range(lens-1,-1,-1):
 9     j+=1
10     if j%3==0 and i-1>=0 and res[i-1]!='-':
11         j=0
12         res.insert(i,',')
13    
14 print(''.join(map(str, res)))
View Code

 

posted @ 2021-07-16 21:58  浅忆~  阅读(42)  评论(0)    收藏  举报