A1001. A+B Format (20)
来源:互联网 发布:淘宝图书讲谈社 编辑:程序博客网 时间:2024/05/22 05:09
1.题目描述
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
2.解题过程
本题并没有涉及大数加减法,只是在输出时需要注意从各位起,每三位添加一个‘,’分割,因此将计算的结果放到字符数组中并添加‘,’。
需要注意的是最高为不要多输出‘,’,另外要特别处理和为0的情况。
代码如下:
/*A1001*/#include<cstdio>#include<cstring>#define NUM 10int main(){ int a,b,sum,count=0,i=0,count2=0;; char ans[NUM]; scanf("%d%d",&a,&b); sum = a + b; if(sum<0){ printf("-"); sum = -sum; } if(sum==0){ ans[0] = 0; printf("0"); } else{ while(sum){ ans[i] = sum%10 + '0'; i++; count++; count2++; sum = sum/10; if(count%3==0&&sum){ ans[i] = ','; i++; count2++; } } } for(int j=count2-1;j>=0;j--){ printf("%c",ans[j]); } return 0;}
阅读全文
0 0
- a1001. A+B Format (20)
- A1001. A+B Format (20)
- A1001. A+B Format (20)
- A1001. A+B Format (20)
- PAT:A1001. A+B Format (20/20)
- 【PAT】A1001. A+B Format (20)
- PAT---A1001. A+B Format (20)
- PAT A1001. A+B Format (20)
- PAT A1001 A+B Format(20)
- PAT A1001. A+B Format
- PAT A1001. A+B Format
- PAT甲级练习题A1001.A+B Format
- PAT A1001 A+B Fomat
- A+B Format (20)
- A+B Format (20)
- A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- Vue.js的认知
- Linux之旅
- [前端与移动开发] 【前端技术分享】Web前端性能优化的9大问题
- Python编程_Lesson001_python简介和输入输出
- 分布式系统SDK端重试策略
- A1001. A+B Format (20)
- 合并区间
- java基础知识点回顾笔记(日更)
- 对文件进行操作
- python新式类和经典类的区别?
- nodejs 连接 MySQL
- (C++)堆栈的类模板
- 留个纪念傻傻的博客
- 链表排序 1.0粗糙版