关于‘1001.A+B Format (20)’的解题报告
2017-01-22 11:51
423 查看
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
“谢谢观赏”一脸满足的小豪如上说到。
(菜鸡小豪的编程历程还在持续做更中......)
相关文章推荐
- PAT甲级1001. A+B Format (20)解题报告
- (2016.2.2)1001.A+B Format (20)解题思路
- 1001. A+B Format (20)的解题思路以及多源代码文件的尝试编写
- 1001.A+B Format (20)解题描述
- PAT (Advanced Level) 1001. A+B Format (20) 解题报告
- POJ 1001 解题报告 Exponentiation
- 【POJ1182】食物链,思路+数据+代码,可能是史上关于这道题最详细的解题报告
- 【原】 POJ 1001 Exponentiation 大整数乘法 解题报告
- 关于POJ1001的解题报告<面向菜鸟>
- POJ 1001(浮点数高精度幂) 解题报告
- 杭电HDOJ 1001 解题报告
- POJ解题报告_1001_Exponentiation
- POJ 1001解题报告
- 1001. A+B Format (20)
- 2013年4月_武科大程序设计大赛_解题报告(problem 1001)
- 2013年4月_武科大程序设计大赛(复赛)_解题报告(problem 1001)
- POJ1001 Exponentiation (解题报告)
- POJ 1001 Exponentiation解题报告
- [解题报告]ZOJ 1001
- hdu 1001 Sum Problem 的解题报告