A + B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7852 Accepted Submission(s): 4417
Problem Description
读入两个小于100的正整数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
直接 把字符串转化为数字。 遇到 '+' 换数字。 遇到 ‘=’ 停止输入。
//Memory: 216 KBTime: 0 MS
//Language: C++Result: Accepted
#include
#include
int main()
{
char a[6][10];
int i,b[2],j,temp,ten,c;
while(scanf("%s",a[0])!=EOF)
{
ten=1;
c=0;
b[0]=b[1]=0;
for(i=0;;i++)
{
if(i!=0)
scanf("%s",a[i]);
if(a[i][0]=='=') break;
if(strcmp(a[i],"zero")==0)
temp=0;
if(strcmp(a[i],"one")==0)
temp=1;
if(strcmp(a[i],"two")==0)
temp=2;
if(strcmp(a[i],"three")==0)
temp=3;
if(strcmp(a[i],"four")==0)
temp=4;
if(strcmp(a[i],"five")==0)
temp=5;
if(strcmp(a[i],"six")==0)
temp=6;
if(strcmp(a[i],"seven")==0)
temp=7;
if(strcmp(a[i],"eight")==0)
temp=8;
if(strcmp(a[i],"nine")==0)
temp=9;
if(a[i][0]=='+')
{
ten=1;
c=1;
}
else
{
b[c]=b[c]*ten+temp;
ten*=10;
}
}
if(b[0]+b[1]==0) break;
printf("%d\n",b[0]+b[1]);
}
return 0;
}