第二次寒假作业其一(代码问题)

1001.A+B Format (20)

代码戳这里
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位加个逗号。
  • 数是有范围的[-1000000,1000000],负数和正数只差个负号,所以只需考虑正数。
  • 发现1,000,000只有两个加逗号的地方,所以用if分为3个部分分别加逗号。
  • 对于小于1000的数直接输出即可;大于等于1000小于1,000,000的数只需去除大于1000的部分就可得到尾数。再使用printf分块输出即可。

以下是我pat的截图


发现错误后就把特殊的情况代进去检验,发现当输入1000和0时输出结果为1,0。漏了几个0,随后在printf的格式上作出相应调整再提交就成功了。

The end
posted @ 2017-02-02 14:33  乐乐kami  阅读(151)  评论(3编辑  收藏  举报