您的位置:首页 > 其它

杭电1228

2014-03-26 17:00 155 查看
这题没有什么水平,但是我没有找到比较好或者是比较巧妙的处理的方法,基本上的AC的答案的思路都是差不多的,但是也有处理的比较好的,这里呢我就不再讲解思路了,因为这个大家可以想出很多的做法,下面就是贴两个别人的比较好可以AC的代码:

代马一:

#include <stdio.h>
#include <string.h>
char num[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int search(char ch[])
{
    int i;
    for(i=0;i<10;i++)
    {
           if(strcmp(ch,num[i])==0)
           break;
    }
    return i;
}
int main()
{
    char count[10];
    int a,b;
    while(1)
    {
            a=0;
            while(scanf("%s",count)&&strcmp(count,"+")!=0)
            a=a*10+search(count);
            b=0;
            while(scanf("%s",count)&&strcmp(count,"=")!=0)
            b=b*10+search(count);
            if(a==0&&b==0) return 0;
            else
            {
                     printf("%d\n",a+b);
            }
    }
    return 0;
}

代码二:

#include <stdio.h>
#include <string.h>
char *wdvalue[]={"zero","one","two","three",
		"four","five","six","seven","eight","nine"};
int search(char s[])
{
	int i;
	for(i=0;i<10&&strcmp(s,wdvalue[i]);i++)
		;
	return i;
}
main()
{
	char s[100];
	int sum,a;
	sum=a=0;
	while(scanf("%s",s)!=EOF){
		if(!strcmp(s,"=")){
			sum+=a;
			if(sum==0)
				break;
			printf("%d\n",sum);
			sum=a=0;
		}else if(!strcmp(s,"+")){
			sum+=a;
			a=0;
		}else
			a=a*10+search(s);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: