A + B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12153 Accepted Submission(s): 7103
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
CODE:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int n;
char a[5000];
char cha[20][20];
while(gets(a))
{
int i,j,k,len;
len=strlen(a);
k=0;
for(i=0;i<len;)
{
for(j=0;;j++)
{
if(a[i]!=' ')
{
cha[k][j]=a[i];
i++;
}
else
{
k++;i++;break;
}
}
}
int t=0,b[1000];
for(i=0;i<k;i++)
{
if(!strcmp(cha[i],"one"))
b[t++]=1;
else if(!strcmp(cha[i],"two"))
b[t++]=2;
else if(!strcmp(cha[i],"three"))
b[t++]=3;
else if(!strcmp(cha[i],"four"))
b[t++]=4;
else if(!strcmp(cha[i],"five"))
b[t++]=5;
else if(!strcmp(cha[i],"six"))
b[t++]=6;
else if(!strcmp(cha[i],"seven"))
b[t++]=7;
else if(!strcmp(cha[i],"eight"))
b[t++]=8;
else if(!strcmp(cha[i],"nine"))
b[t++]=9;
else if(!strcmp(cha[i],"zero"))
b[t++]=0;
else if(!strcmp(cha[i],"+"))
b[t++]=-1;
else
continue;
}
//printf("%d@",b[1]);
int temp;
for(i=0;i<t;i++)
if(b[i]==-1)
{
temp=i;break;
}
//printf("%d!",temp);
int A,B;
A=B=0;
for(i=0;i<temp;)
if(b[i]==0)
i++;
else break;
for(;i<temp;i++)
if(temp-i==2)
A+=10*b[i];
else
A+=b[i];
//printf("%d&",A);
for(i=temp+1;i<t;)
if(b[i]==0)
i++;
else break;
for(;i<t;i++)
if(t-i==2)
B+=10*b[i];
else
B+=b[i];
//printf("%d~",B);
if(!(A||B))
break;
printf("%d\n",A+B);
memset(a,'\0',sizeof(a));
memset(cha,'\0',sizeof(cha));
}
return 0;
}