PAT 1001 A+B Fotmat

源码

1001. A+B Format (20)  
时间限制 400 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard 
作者 CHEN, Yue

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

解题过程

  • 题意:求A+B的值,并以标准格式输出,即从右往左每3位数用','隔开。
  • 思考过程:先不考虑正负,最后再加上符号。最开始想利用数组,将各个位上的数存入一个数组中,但我想到的处理方法太繁琐,所以放弃这种方法。后来想到只要将每3位分割出来就可以了。因为 -1000000 <= a, b <= 1000000,所以-2000000<a+b<2000000,最多就出现x,xxx,xxx这种情况。后3位数abs(a+b)%1000,中间3位数abs(a+b)/1000%1000,前面那一位数abs(a+b)/1000000。
  • 编码完后的调试:选取比较特别的几个数自己进行测试。例如 (0) (xx) (x,xxx) (x,xxx,xxx),最初由于if中的条件没写好会出现没有输出或者多组输出的情况,对条件修改后没有这种情况发生。将这些问题解决后提交OJ,一次通过。


PDF

posted @ 2017-01-19 21:26  Dark-Existed  阅读(243)  评论(1编辑  收藏  举报