PAT 1001
来源:互联网 发布:汽车仪表单片机 编辑:程序博客网 时间:2024/05/22 23:58
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 9Sample Output
-999,991代码主要注意两点 :
- 输出时要注意逗号,如1000要输出1,000
- 要注意后面部分数字的前置0,如1010,要输出1,010,而不是1,10
1 #include <stdio.h>
2 int main()
3 {
4 int a,b,sum;
5 int arr[5];
6 while(scanf("%d%d",&a,&b) != EOF){
7 sum = a+b;
8 if(sum == 0){
9 printf("0\n");
10 break;
11 }
12 else if(sum < 0){
13 printf("-");
14 sum = sum * -1;
15 }
16 int i = 0;
17 while(sum){
18 arr[i++] = sum % 1000;
19 sum = sum / 1000;
20 }
21 printf("%d",arr[--i]);
22 for(--i;i >= 0;--i){
23 printf(",%03d",arr[i]);
24 }
25 printf("\n");
26 }
27 return 0;
28 }
2 int main()
3 {
4 int a,b,sum;
5 int arr[5];
6 while(scanf("%d%d",&a,&b) != EOF){
7 sum = a+b;
8 if(sum == 0){
9 printf("0\n");
10 break;
11 }
12 else if(sum < 0){
13 printf("-");
14 sum = sum * -1;
15 }
16 int i = 0;
17 while(sum){
18 arr[i++] = sum % 1000;
19 sum = sum / 1000;
20 }
21 printf("%d",arr[--i]);
22 for(--i;i >= 0;--i){
23 printf(",%03d",arr[i]);
24 }
25 printf("\n");
26 }
27 return 0;
28 }
0 0
- pat 1001
- pat 1001
- PAT 1001
- PAT 1001
- PAT 1001
- PAT 1001
- PAT 1001
- PAT 1001
- PAT 1001
- PAT 1001
- PAT-1001
- PAT 1001
- PAT 1001
- PAT 1001
- PAT-1001
- PAT 1001
- PAT (BL) 1001
- PAT 1001-1010 题解
- 第三周项目1-动态链表体验(一)
- 配置Oracle dbconsole的密钥
- smarthosts的Mac安装方法
- c语言目录操作总结
- 写在最前
- PAT 1001
- PAT 1002
- PAT 1003
- Jordan Lecture Note-1: Introduction
- PAT 1004
- PAT 1005
- ASP.NET的Cookie
- PAT 1006
- PAT 1007