您的位置:首页 > 其它

关于‘1001.A+B Format (20)’的解题报告

2017-01-22 11:51 417 查看

1001.A+B Format(20)

首先要感谢一下指导我github上传问题的小伙伴们,捣腾了一整天我终于摸到了一点门路,真的谢谢你们。

小豪的github

问题描述:

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和a+b的数据落在[-2000000,2000000]区间内,也就是说在输出时,数据最多的情况需要将其分成三组。

剩下的问题就是在格式的需要上,每三个数位为一组,这就要求不足三位的情形有时候需要用“0”补齐。

‘A+B Format’代码

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,sum;
scanf("%d%d",&a,&b);
sum=a+b;
if(abs(sum)>=1000000)
printf("%d,%03d,%03d",sum/1000000,abs((sum/1000)%1000),abs(sum%1000));
else if(abs(sum)<1000)
printf("%d",sum);
else
printf("%d,%03d",sum/1000,abs(sum%1000));
return 0;
}

提交记录



回看代码,发现了一些小细节的疏忽。



在纠正了代码里小于等于三个数位的输出格式问题以及分类的区间分界问题以后,提交通过。



附上我的PDF:小豪的pdf

“谢谢观赏”一脸满足的小豪如上说到。

(菜鸡小豪的编程历程还在持续做更中......)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: