Skip to content

Commit 8970225

Browse files
committed
advanced level a1001
1 parent f69cabc commit 8970225

File tree

1 file changed

+62
-0
lines changed
  • CCodes/PatQuestionBank/AdvancedLevel

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
1001 A+B Format(20 分)
3+
4+
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).
5+
Input Specification:
6+
7+
Each input file contains one test case. Each case contains a pair of integers a and b where −10​6​​≤a,b≤10​6​​. The numbers are separated by a space.
8+
Output Specification:
9+
10+
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.
11+
Sample Input:
12+
13+
-1000000 9
14+
15+
Sample Output:
16+
17+
-999,991
18+
19+
*/
20+
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
#include <string.h>
24+
25+
int main ()
26+
{
27+
int a, b, i, cnt = 0, cc = 0;
28+
int sum, str_len;
29+
int flag = 0;
30+
char s[20], c[20];
31+
32+
if (!scanf("%d %d", &a, &b)) {
33+
return 1;
34+
}
35+
36+
sum = a + b;
37+
38+
if (sum < 0) {
39+
flag = 1;
40+
}
41+
sprintf(s, "%d", sum);
42+
43+
str_len = strlen(s);
44+
45+
for (i = str_len - 1; i >= 0; i--) {
46+
c[cnt++] = s[i];
47+
cc++;
48+
if (cc % 3 == 0) {
49+
if (i != 0) {
50+
if (!flag || i != 1) {
51+
c[cnt++] = ',';
52+
cc = 0;
53+
}
54+
}
55+
}
56+
}
57+
58+
for (i = cnt - 1; i >= 0; i--) {
59+
printf("%c", c[i]);
60+
}
61+
return 0;
62+
}

0 commit comments

Comments
 (0)