方法一(算法书上的方法)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char eng[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int n,end=0,num[10];
printf("输入你要翻译的数字:");
scanf("%d",&n);
int temp=n;
while(temp!=0){
num[end++]=temp%10;
temp/=10;
}
end--;
for(;end>0;end--){
printf("%s-",eng[num[end]]);
}
printf("%s",eng[num[0]]);
return 0;
}
方法二(自己瞎写)
#include <stdio.h>
int main(){
char ch;
int cnt=0;
char str[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int num[10];
printf("将输入的数字转换成字符串:");
for(int i=0;ch!='\n';i++){
ch=getchar();
num[i]=ch-'0';
cnt++;
}
printf("%s",str[num[0]]);
for(int j=1;j<cnt-1;j++){
printf("-%s",str[num[j]]);
}
return 0;
}
在时间复杂度上前者要远低于后者。