PAT: 1002 Write this number (C language version)

Reads a positive integer n, to calculate the sum of the digits, each digit and write Pinyin.

Input format:
Each test comprises a test input, i.e., given the value of the natural number n. Where n is less than 10 to ensure
100
.

Output formats:
between each one, Pinyin digital output in a row n of the sum of the digits 1 spaces, but after the last row Pinyin digital with no spaces.

Sample input:
1234567890987654321123456789

Sample output:
yi wu San

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

int sum(char *c);
void NumtoPinyin(int n);

int main()
{
    int total = 0;
    char c[101];

    gets(c);
    total = sum(c);
    NumtoPinyin(total);

    return 0;
}
int sum(char *c)
{
    int i, n, total = 0;

    i = 0;
    while(c[i] != '\0')
        total = total + c[i++] - '0';

    return total;
}
void NumtoPinyin(int n)
{
    int a[10], i = 0;
    char *pinyin[10] = {"ling", "yi", "er", "san", "si", "wu",
    "liu", "qi", "ba", "jiu"};

    while(n != 0)
    {
        a[i++] = n%10;
        n = n / 10;
    }
    --i;
    while(i >= 0)
    {
        if (i == 0)
        {
            printf("%s", pinyin[a[i]]);
            break;
        }
        printf("%s ", pinyin[a[i]]);
        i--;
    }
}
Published 24 original articles · won praise 0 · Views 160

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105041903