sprintf 函数的使用会使这道题简单很多
View Code
1 #include<stdio.h> 2 #include<string.h> 3 void pt(char a) 4 { 5 switch (a) 6 { 7 case '0': printf("zero"); break; 8 case '1': printf("one"); break; 9 case '2': printf("two");break; 10 case '3': printf("three"); break; 11 case '4': printf("four"); break; 12 case '5': printf("five"); break; 13 case '6': printf("six"); break; 14 case '7': printf("seven"); break; 15 case '8': printf("eight"); break; 16 case '9': printf("nine"); break; 17 } 18 } 19 void print(char a[], int length){ 20 21 pt(a[0]); 22 for (int j = 1; j < length; j++) 23 { 24 printf(" "); 25 pt(a[j]); 26 } 27 } 28 int main() 29 { 30 char a[105]; 31 int length, sum ; 32 int i,flag = 0; 33 while (scanf("%s", a) != EOF) 34 { 35 sum = 0; 36 for(i = 0; i< strlen(a); i++) 37 sum = sum + a[i] - '0' ; 38 length = sprintf(a, "%d", sum); 39 40 if(flag == 1) printf("\n"); 41 else 42 flag = 1; 43 print(a, length); 44 } 45 46 return 0; 47 }