1005 Spell It Right【PAT (Advanced Level) Practice】

1005 Spell It Right【PAT (Advanced Level) Practice】

Original question link:Preview question details - 1005 Spell It Right (pintia.cn)

1.Original text of the title

Given a non-negative integer N N N, your task is to compute the sum of all the digits of N N N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N N N (10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

2. Title translation

Given a non-negative integer N N N, your task is to calculate N N The sum of all numbers of N and outputs each number of the sum in English.

Input specifications:

Each input file contains one test case. Each case occupies a row containing a N N N( ≤ 1 0 100 \le 10^{100}10100)。

Output specification:

For each test case, output the English word for the sum of the number in one line. There must be a space between two consecutive words, but no additional spaces at the end of the line.

Example input:

12345

Example output:

one five

3. Problem-solving ideas

3.1 Question analysis

Calculate the sum of the digits of a non-negative integer and output each digit of the sum as an English word.

3.2 Basic ideas

Convert the integer to a string, then iterate through the string to calculate the sum of each digit, and output each digit of the sum as an English word.

3.3 Detailed steps

  1. Define function i_to_s:

    • Convert an integer to a string and store it in a character array.
  2. Main function main:

    • Reads a string representation of a number from standard input.
    • Calculate the sum of the numbers.
    • Call a function to convert the sum to a string.
    • Output the English words of each digit, separated by spaces, and finally newline.

4. Reference answer

#include <stdio.h>
#include <string.h>

// 将整数转换为字符串
void i_to_s(int sum, char n[]) {
    
    
    int d, i;

    i = 0;
    // 取最低位的数字,并将其转换为字符存入数组
    d = sum % 10;
    sum /= 10;
    while (sum) {
    
    
        n[i++] = d + '0';
        d = sum % 10;
        sum /= 10;
    }
    n[i++] = d + '0';
    n[i] = '\0';  // 字符串末尾加上'\0'表示字符串结束
}

int main() {
    
    
    char n[102];
    int sum, i;

    scanf("%s", n);
    sum = 0;
    // 计算输入数字的各位数字之和
    for (i = 0; n[i] != '\0'; i++)
        sum += n[i] - '0';

    i_to_s(sum, n);
    i = strlen(n) - 1;

    // 将和的每一位数字以英文单词输出
    while (i != -1) {
    
    
        switch (n[i--] - '0') {
    
    
            case 0: printf("zero"); break;
            case 1: printf("one"); break;
            case 2: printf("two"); break;
            case 3: printf("three"); break;
            case 4: printf("four"); break;
            case 5: printf("five"); break;
            case 6: printf("six"); break;
            case 7: printf("seven"); break;
            case 8: printf("eight"); break;
            case 9: printf("nine"); break;
        }
        if (i != -1)
            printf(" ");
        else
            printf("\n");
    }

    return 0;
}

Guess you like

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