PAT 1002 (2)

PAT 1002 (2)

Title description:

读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

Enter a description:

每个测试输入包含 1 个,即给出自然数 n 的值。这里保证 n 小于 10的100次方 。

Output description:

在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。

Input example:

1234567890987654321123456789

Example output:

yi san wu

Code:

/*写出这个数*/

#include<iostream>

using namespace std;

void print1(int x)
{
    switch(x){
    case 1:
        printf("yi");
        break;
    case 2:
        printf("er");
        break;
    case 3:
        printf("san");
        break;
    case 4:
        printf("si");
        break;
    case 5:
        printf("wu");
        break;
    case 6:
        printf("liu");
        break;
    case 7:
        printf("qi");
        break;
    case 8:
        printf("ba");
        break;
    case 9:
        printf("jiu");
        break;
    case 0:
        printf("ling");
        break;
    }
}
//递归算法,将最终得到的各数字之和转换成相应拼音
void print2(int x){
    if(x)
    {
        print2(x/10);//和整除
        if(x>9)
            printf(" ");
        print1(x%10);//和取余
    }
}

int main()
{
    char a[101];
    scanf("%s",a);//输入为字符型数据
    int i=0;
    long long ans=0;
    while(a[i])
    {
        ans+=a[i]-'0';//将字符型数据转换为整型,并用一个变量累加起来,在ACSII码表中,字符‘0’的十进制值是48
        i++;
    }
     print2(ans);
     return 0;

}

Guess you like

Origin blog.csdn.net/Duba_zhou/article/details/126570687