-
虽然题目中的最大值设置为了2000000,但程序中选择大于该值且为3的倍数的数组大小==9
-
将输入数的每一位计算出来保存到数组m中
-
使用两个循环,第一个循环控制单位(million or thousand)范围0-2,第二个循环控制每个单位的百十个(范围也是0-2),使用这两个计算出当前读取的m的索引
-
注意十位上为1,个位上为0和非0的的情况,应在两个判断中分别处理
#include<stdio.h>
int main(){
int in;
char s2[10][10] = {"zero", "one", "two", "three", "four","five", "six", "seven", "eight", "nine"};
char s1[10][10] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};
char s0[10][10] = {"ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
scanf("%d",&in);
int m[9];
int k=8;
while(k>=0){
m[k] = in%10;
in = in/10;
k--;
}
int f_and = 0;
for(int i=0;i<3;i++){
int sum = 0;
for(int j=0;j<3;j++){
int idx = i*3+j;
sum += m[idx];
if(m[idx]!=0){
if(j==0){
printf("%s hundred ",s2[m[idx]]);
f_and = 1;
}
else if(j==1){
if(f_and==1){
printf("and ");
f_and = 0;
}
if(m[idx]>1) //输出>=20的整10数
printf("%s ",s0[m[idx]-1]);
else if(m[idx]==1 && m[idx+1]==0){ // ten
printf("ten ");
}
}
else{
if(f_and==1){
printf("and ");
f_and = 0;
}
if(m[idx-1]==1){
printf("%s ",s1[m[idx]]);
}
else{
printf("%s ",s2[m[idx]]);
}
}
}
}
if(sum!=0){
if(i==0)
printf("million ");
else if(i==1)
printf("thousand ");
}
}
}
#include<stdio.h> int main(){ int in; char s2[10][10] = {"zero", "one", "two", "three", "four","five", "six", "seven", "eight", "nine"}; char s1[10][10] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; char s0[10][10] = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; scanf("%d",&in); int m[9]; int k=8; while(k>=0){ m[k] = in%10; in = in/10; k--; } int f_and = 0; for(int i=0;i<3;i++){ int sum = 0; for(int j=0;j<3;j++){ int idx = i*3+j; sum += m[idx]; if(m[idx]!=0){ if(j==0){ printf("%s hundred ",s2[m[idx]]); f_and = 1; } else if(j==1){ if(f_and==1){ printf("and "); f_and = 0; } if(m[idx]>1) //输出>=20的整10数 printf("%s ",s0[m[idx]-1]); else if(m[idx]==1 && m[idx+1]==0){ // ten printf("ten "); } } else{ if(f_and==1){ printf("and "); f_and = 0; } if(m[idx-1]==1){ printf("%s ",s1[m[idx]]); } else{ printf("%s ",s2[m[idx]]); } } } } if(sum!=0){ if(i==0) printf("million "); else if(i==1) printf("thousand "); } } }