1002. Write this number

Read in a natural number n, calculate the sum of its digits, and write each digit of the sum in Chinese Pinyin.
Input format:
Each test input contains 1 test case, that is, the value of the natural number n is given. Here n is guaranteed to be less than 10100.
Output format:
output each digit of the sum of the digits of n in one line, there is a space between the pinyin numbers, but there is no space after the last pinyin number in a line.
Input example:
1234567890987654321123456789
Output example:
yi san wu
Note: The idea of ​​this question is very simple, read characters-accumulate-output characters. The definition of character array can use char *[], otherwise use two-dimensional, one-dimensional table numbers, and two-dimensional represent the length of the stored character corresponding to each character.

#include<stdio.h>
#include<string.h>
int main()
{
    char c;
    int sum = 0;
    char a[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};//字符要用双引号
    while ((c=getchar())!='\n') sum += c-'0';//一直输入数字直到回车代表结束,'0'代表0是以ASCII码参与运算
    if (sum/100)//判断百位是否为零
        printf("%s ", a[sum/100]);//输出百位
    if (sum/10)//判断十位是否为零
        printf("%s ", a[sum/10%10]);//输出十位
    printf("%s", a[sum%10]);//一位数不需要判断直接输出
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324631629&siteId=291194637