PTA甲级1001 A+B Format

PTA甲级1001 A+B Format

#多种情况的考虑,如果使用字符串,要考虑负号对整体的影响
a,b = map(int,input().split())
s = str(a+b)
pan =True
if int(s)<0 :
    pan = False
    s = s[1:]
if len(s)>=4:
    sum,start = divmod(len(s),3)
    pos= [start]
    for i in range(1,sum):
        pos.append(start+i*3)
    for i in range(1,len(s)+1):
        if i == 1 and pan == False:
            print('-',end='')
        if i in pos:
            print(s[i-1],',',sep='',end='')
        else:
            print(s[i-1],end='')
elif pan:
    print(s)
else:
    print('-',s,sep='')

你可能感兴趣的:(PTA甲级,python学习,python)