读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。
输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。
输入样例:1234567890987654321123456789输出样例:
yi san wu
AC代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 101
int main(){
char s[SIZE];
while(gets(s) != NULL){
//find sum
int sum = 0;
for(int i = 0; i < strlen(s); i++){
sum += s[i] - '0';
}
//store sum in an array
int a[100];
int i;
//specail case for 0
if(sum == 0){
printf("ling\n");
}else{
//generic cases when sum is bigger than 0
for(i = 0; sum > 0; i++){
a[i] = sum%10;
sum /= 10;
}
i--;
for(int j = i; j >= 0; j--){
if(j > 0){
switch(a[j]){
case 0: printf("ling ");break;
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 ");
}
}else{
switch(a[j]){
case 0: printf("ling");break;
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");
}
}
}
printf("\n");
}
}
return 0;
}