Reputation: 508
what I met is a code question below: https://www.patest.cn/contests/pat-a-practise/1001
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
This is my code below:
if __name__ == "__main__":
aline = input()
astr,bstr = aline.strip().split()
a,b = int(astr),int(bstr)
sum = a + b
sumstr= str(sum)
result = ''
while sumstr:
sumstr, aslice = sumstr[:-3], sumstr[-3:]
if sumstr:
result = ',' + aslice + result
else:
result = aslice + result
print(result)
And the test result turn out to be :
时间(Time) 结果(test result) 得分(score) 题目(question number)
语言(programe language) 用时(ms)[time consume] 内存(kB)[memory] 用户[user]
8月22日 15:46 部分正确[Partial Correct](Why?!!!) 11 1001
Python (python3 3.4.2) 25 3184 polar9527
测试点[test point] 结果[result] 用时(ms)[time consume] 内存(kB)[memory] 得分[score]/满分[full credit]
0 答案错误[wrong] 25 3056 0/9
1 答案正确[correct] 19 3056 1/1
10 答案正确[correct] 18 3184 1/1
11 答案正确[correct] 19 3176 1/1
2 答案正确[correct] 17 3180 1/1
3 答案正确[correct] 16 3056 1/1
4 答案正确[correct] 14 3184 1/1
5 答案正确[correct] 17 3056 1/1
6 答案正确[correct] 19 3168 1/1
7 答案正确[correct] 22 3184 1/1
8 答案正确[correct] 21 3164 1/1
9 答案正确[correct] 15 3184 1/1
Upvotes: 1
Views: 83
Reputation: 1723
Notice the behavior of your code when you input -1000 and 1. You need to handle the minus sign, because it is not a digit.
Upvotes: 1
Reputation: 71
I can give you a simple that doesn't match answer, when you enter -1000000, 9 as a, b in your input, you'll get -,999,991.which is wrong.
To get the right answer, you really should get to know format in python.
To solve this question, you can just write your code like this.
if __name__ == "__main__":
aline = input()
astr,bstr = aline.strip().split()
a,b = int(astr),int(bstr)
sum = a + b
print('{:,}'.format(sum))
Upvotes: 3