1001 A+B Format【PAT (Advanced Level) Practice】

1001 A+B Format【PAT (Advanced Level) Practice】

Original question link:Preview question details - 1001 A+B Format (pintia.cn)

1.Original text of the title

Calculate a + b a + b 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 Specification:

Each input file contains one test case. Each case contains a pair of integers a a a and b b b where − 1 0 6 ≤ a , b ≤ 1 0 6 -10^6 \le a, b \le 10^6 106a,b106. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a a a and b b b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

2. Title translation

计算 a + b a + b a+b and outputs the result in standard format, i.e. the numbers must be comma-separated into groups of three digits (unless the number has fewer than four digits).

Input specifications:

Each input file contains one test case. Each test case contains a pair of integers a a a sum b b b, and one − 1 0 6 ≤ a , b ≤ 1 0 6 -10^6 \le a, b \le ^6 106a,b106. These two numbers are separated by a space.

Output specification:

For each test case, you should output in one line a a a sum b b The sum of b. and must be written in a standard format.

Example input:

-1000000 9

Example output:

-999,991

3. Problem-solving ideas

3.1 Question analysis

  1. Calculate two integers a Sum b Object sumc, Yu − 1 0 6 ≤ a , b ≤ 1 0 6 -10^6 \le a, b \le 10^6 106a,b106 We know that the sum of a and b c does not exceed the integer type range of values.
  2. Output the result in standard format, which requires commas to separate every three digits of the number, unless the number is less than four digits, as shown in the output sample, cUp to 7 Number of digits, up to two commas.

3.2 Basic ideas

Read input, sum, and output according to format requirements.

3.3 Detailed steps

  1. Read two integersa and b from standard input.
  2. Calculate their sumc.
  3. Output according to the format requirements:
    • as a result -1000 to 1000 between (inclusive -1000 sum 1000 ), direct exportc.
    • otherwise:
      • First determine whether the sumc is positive or negative, output a negative sign for negative numbers, and then convert the negative number into a positive number for processing.
      • stores each digit of the sum c in the character array s.
      • Traverse the array elements and output them bit by bit, outputting a comma for every three digits to meet the requirements of the standard format.

3.4 Points to note

A comma is output for every three digits. There is no need to use three as a circular increment to find the output position, because the maximum is 7 digits and a maximum of two commas. If the third or sixth digit is to be output, the comma is output first. That’s it.

4. Reference answer

#include<stdio.h>

int main(){
    
    
    int a, b, c, i;
    char s[80];

    // 从标准输入读取两个整数
    scanf("%d %d", &a, &b);

    // 计算和
    c = a + b;

    // 判断和的范围,如果在 -1000 到 1000 之间(不包括 -1000 和 1000),直接输出
    if  ((c < 1000) && (c > -1000))
        printf("%d\n", c);
    else {
    
    
        if (c < 0) {
    
    
            printf("-");
            c = -c; // 将负数转为正数以便处理
        }
        i = 0;

        // 将和的每一位数字存储在字符数组 s 中
        while (c) {
    
    
            //对10取余获得最低位,然后转换为对应字符,实现逆序存储
            s[i++] = '0' + c % 10;
            c /= 10;//舍去最低位数字
        }

        i--;

        // 从数组最后往前输出,先输出数组最后一位,即 C 的最高位
        printf("%c", s[i--]);

        // 每三位输出一个逗号
        for (; i >= 0; i--) {
    
    
            //若将要输出的是第三或第六位数字,则先输出逗号
            if ((i == 5) || (i == 2))
                printf(",");
            printf("%c", s[i]);
        }
        printf("\n");
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_40171190/article/details/134725362