PTA乙级1002 写出这个数 (20分)
题目原文
读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式:
每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10100。
输出格式:
在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。
输入样例:
1234567890987654321123456789
输出样例:
yi san wu
代码
#include<iostream>
#include<cstring>
using namespace std;
void Prt(char a) {
if (a == '0')
cout << "ling";
else if(a == '1')
cout << "yi";
else if (a == '2')
cout << "er";
else if (a == '3')
cout << "san";
else if (a == '4')
cout << "si";
else if (a == '5')
cout << "wu";
else if (a == '6')
cout << "liu";
else if (a == '7')
cout << "qi";
else if (a == '8')
cout << "ba";
else if (a == '9')
cout << "jiu";
}
int main(void) {
char Arry[102];
char Result[4];
int i=0,sum=0;
cin >> Arry;
while (Arry[i]!='\0'){
sum += Arry[i++] - '0';
}
i = 0;
while (sum) {
Result[i++] = sum % 10+'0';
sum /= 10;
}
for (int j = i - 1; j >= 0; j--) {
if (j != i - 1) cout << " ";
Prt(Result[j]);
}
return 0;
}

浙公网安备 33010602011771号