A + B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12596 Accepted Submission(s): 7378
Problem Description
读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.
需要注意的是:A和B的每一位数字由对应的英文单词给出.
Input
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
Output
对每个测试用例输出1行,即A+B的值.
Sample Input
one + two = three four + five six = zero seven + eight nine = zero + zero =
Sample Output
3 90 96
解题思路
用gets()!=NULL输入并存储,然后判断a到‘+’截至,然后判断b到‘=’截至,最后输出a+b之和。
代码
#include<stdio.h>
#include<string.h>
#include<string.h>
char s[1100];
char num[1100];
int switcha(char c[])
{
if(strcmp(c,"zero")==0)
return 0;
else if(strcmp(c,"one")==0)
return 1;
else if(strcmp(c,"two")==0)
return 2;
else if(strcmp(c,"three")==0)
return 3;
else if(strcmp(c,"four")==0)
return 4;
else if(strcmp(c,"five")==0)
return 5;
else if(strcmp(c,"six")==0)
return 6;
else if(strcmp(c,"seven")==0)
return 7;
else if(strcmp(c,"eight")==0)
return 8;
else if(strcmp(c,"nine")==0)
return 9;
}
int main()
{
int a,b;
int i,j,k;
int len;
while(gets(s))
{
len=strlen(s);
a=0;
for(i=0,j=0;s[i]!='+';i++)
{
if(s[i]!=' ')
{
num[j]=s[i];
j++;
}
else
{
a=a*10+switcha(num);
j=0;
memset(num,0,sizeof(num));
//注意用完之后清零,避免这一次three,写一次one结果存储为oneee的情况
}
}
k=i+2;
b=0;
for(i=k,j=0;s[i]!='=';i++)
{
if(s[i]!=' ')
{
num[j]=s[i];
j++;
}
else
{
b=b*10+switcha(num);
j=0;
memset(num,0,sizeof(num));
}
}
if(a==0&&b==0)
break;
printf("%d\n",a+b);
}
return 0;
}