PAT (Advanced Level) Practise:1001. A+B Format
2015-09-01 14:40
471 查看
【题目链接】
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
Sample Output
题意分析:
输出(a+b)的结果,输出格式需要使用逗号把数值分割。
提交代码:
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)的结果,输出格式需要使用逗号把数值分割。
提交代码:
#include <stdio.h> int main(void) { int out[10]; int i, len, tmp; int a, b, c; scanf("%d %d", &a, &b); c = a + b; if(c < 0) { printf("-"); c = -c; } len = 0; do { out[len] = c % 10; c /= 10; len++; } while(c != 0); for(i = 0; i < len; i++) { printf("%d", out[len-1-i]); tmp = len - i - 1; //if((len-i-1)%3 == 0 && (len-i) > 1) if(tmp != 0 && tmp % 3 == 0) printf(","); } return 0; }
相关文章推荐
- pscp 实现windows linux间互传文件
- 0901Android基础仿QQ聊天界面(下)
- 1045. Favorite Color Stripe (30)
- 关于使用tpl模板如何从模板中取数据放入js中
- flex datagrid水平或者竖直的问题
- 串口编程--java层开发demo
- android 学习之获取application和activity下meta-data中的值
- MFC 调试方法
- 配置免SSH
- Linux命令(用到再更新)
- bzoj1578Stock Market(dp)
- UIImageView响应点击事件
- Eclipse设置编译文件.class输出路径
- c primer plus第十一章课后习题
- leetCode #286 Missing Number
- IOS获取文件路径的方法
- datagrid锁定列的位置
- 学习总结1
- IOS和Android支持的音频编解码
- android菜鸟进阶之路——播放音乐和视频