当前位置: 代码迷 >> C语言 >> 一道C语言的难题
  详细解决方案

一道C语言的难题

热度:214   发布时间:2007-04-17 18:49:22.0

这个题目明显用数组比用switch简单


----------------解决方案--------------------------------------------------------
#include<stdio.h>
void main()
{
char a[10][10]={"zero","one","tow","tree","four","five","six","seven","eght","nine"};
int i;
char b[10];
while(scanf("%s",b)!=EOF)
{
for(i=0;b[i];i++)
printf("%s ",a[b[i]-'0']);
printf("\n");
}
}
----------------解决方案--------------------------------------------------------
用指针,或者数组,加个switch就可以了,呵呵好心都搞定了啊。
----------------解决方案--------------------------------------------------------
crackerwang
一直拉下来看,终于看到一个是这样做的.

----------------解决方案--------------------------------------------------------

我改了以下你的代码
#include<stdio.h>
void main()
{
char a[10][10]={"zero","one","tow","tree","four","five","six","seven","eght","nine"};
int i;
while(scanf("%1d",&i)!=EOF)
{
printf("%s ",a[i]);
}
}


----------------解决方案--------------------------------------------------------

mp3改的好简洁,佩服


----------------解决方案--------------------------------------------------------
厉害.
我怎么就没有想到呢?
高手就是高手
----------------解决方案--------------------------------------------------------
以下是引用mp3aaa在2007-4-17 21:59:40的发言:

我改了以下你的代码
#include<stdio.h>
void main()
{
char a[10][10]={"zero","one","tow","tree","four","five","six","seven","eght","nine"};
int i;
while(scanf("%1d",&i)!=EOF)
{
printf("%s ",a[i]);
}
}

貌似还不符题义.

#include<stdio.h>
void main()
{
char a[10][10]={"zero","one","tow","tree","four","five","six","seven","eght","nine"};
char c;
while((c=getchar())!='\0')
{
printf("%s ",a[c-'0']);
}
}


----------------解决方案--------------------------------------------------------
呵呵nuciewth斑竹 scanf("%1d",&amp;i) 我这里用的是 1d 也就是说每次有只有1位数存入i
----------------解决方案--------------------------------------------------------
斑竹看错了估计看成了ld了 long int
哈哈
ld
1d
谁能用眼睛分的清楚
----------------解决方案--------------------------------------------------------
  相关解决方案