补充重写版
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
1 |
12345 |
Sample Output:
1 |
one five |
实际为PAT-Basic-1002的英文版,改拼音为单词就okay了,20秒即可AC……
然而这么长长的一串switch和case真的是太难看了……所以顺手又写了一遍简单的版本。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include<stdio.h>//改自PAT-B-1002 #include<math.h> main() { int c,sum,sum_c,digit,num; sum=0; while((c=getchar())!='\n') { sum+=(c-'0'); } sum_c=sum; digit=0; do { sum_c/=10; digit++; } while(sum_c); while(digit) { num=sum/pow(10,digit-1); if(digit==1) { switch(num) { case 1:printf("one");break; case 2:printf("two");break; case 3:printf("three");break; case 4:printf("four");break; case 5:printf("five");break; case 6:printf("six");break; case 7:printf("seven");break; case 8:printf("eight");break; case 9:printf("nine");break; case 0:printf("zero");break; } break; } switch(num) { case 1:printf("one ");break; case 2:printf("two ");break; case 3:printf("three ");break; case 4:printf("four ");break; case 5:printf("five ");break; case 6:printf("six ");break; case 7:printf("seven ");break; case 8:printf("eight ");break; case 9:printf("nine ");break; case 0:printf("zero ");break; } sum=sum-num*pow(10,digit-1); digit--; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<stdio.h> int main() { char num[102],word[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"}; int sum,i; scanf("%s",num); for(i=0;num[i];i++) { sum+=(num[i]-'0'); } sprintf(num,"%d\0",sum); printf("%s",word[num[0]-'0']); for(i=1;num[i];i++) printf(" %s", word[num[i]-'0']); } |