one+two=3,该如何处理
one+two=3
这个题完全没有思路,不知道从哪里下手
先发下描述
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 Input
3
90
96
------解决方案--------------------
#include "stdio.h"
#include "string.h"
int swp(char a[10]);
int scan(void);
int main(int argc, char* argv[])
{
printf("input english number:\n");
int s1 = scan();
int s2 = scan();
printf("%d\n",s1+s2);
return 0;
}
int scan(void)
{
char a[10];
int g0,g1;
scanf("%s",a);
g0 = swp(a);
scanf("%s",a);
if((strcmp(a,"+")==0)||(strcmp(a,"=")==0))
{
return g0;
}
else
{
g1 = swp(a);
}
scanf("%s",a);
if((strcmp(a,"+")==0)||(strcmp(a,"=")==0))
{
return g0*10+g1;
}
}
int swp(char a[10])
{
int c;
if (strcmp(a,"zero")==0) c=0;
else if(strcmp(a,"one")==0) c=1;
else if(strcmp(a,"two")==0) c=2;
else if(strcmp(a,"three")==0) c=3;
else if(strcmp(a,"four")==0) c=4;
else if(strcmp(a,"five")==0) c=5;
else if(strcmp(a,"six")==0) c=6;
else if(strcmp(a,"seven")==0) c=7;
else if(strcmp(a,"eight")==0) c=8;
else if(strcmp(a,"nine")==0) c=9;
return c;
}
------解决方案--------------------
那我也来写一个吧:
这个题完全没有思路,不知道从哪里下手
先发下描述
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 Input
3
90
96
------解决方案--------------------
#include "stdio.h"
#include "string.h"
int swp(char a[10]);
int scan(void);
int main(int argc, char* argv[])
{
printf("input english number:\n");
int s1 = scan();
int s2 = scan();
printf("%d\n",s1+s2);
return 0;
}
int scan(void)
{
char a[10];
int g0,g1;
scanf("%s",a);
g0 = swp(a);
scanf("%s",a);
if((strcmp(a,"+")==0)||(strcmp(a,"=")==0))
{
return g0;
}
else
{
g1 = swp(a);
}
scanf("%s",a);
if((strcmp(a,"+")==0)||(strcmp(a,"=")==0))
{
return g0*10+g1;
}
}
int swp(char a[10])
{
int c;
if (strcmp(a,"zero")==0) c=0;
else if(strcmp(a,"one")==0) c=1;
else if(strcmp(a,"two")==0) c=2;
else if(strcmp(a,"three")==0) c=3;
else if(strcmp(a,"four")==0) c=4;
else if(strcmp(a,"five")==0) c=5;
else if(strcmp(a,"six")==0) c=6;
else if(strcmp(a,"seven")==0) c=7;
else if(strcmp(a,"eight")==0) c=8;
else if(strcmp(a,"nine")==0) c=9;
return c;
}
------解决方案--------------------
那我也来写一个吧:
- C/C++ code
#include <iostream> #include <string> using namespace std; int main() { string word; int i, a[2], b[2]; int first, second, sum; while(1) { a[1] = a[2] = b[1] = b[2] = 10; i = 0; while(cin >> word) { if("zero" == word) a[i] = 0; if("one" == word) a[i] = 1; else if("two" == word) a[i] = 2; else if("three" == word) a[i] = 3; else if("four" == word) a[i] = 4; else if("five" == word) a[i] = 5; else if("six" == word) a[i] = 6; else if("seven" == word) a[i] = 7; else if("eight" == word) a[i] = 8; else if("nine" == word) a[i] = 9; else if("+" == word) break; i++; } i = 0; while(cin >> word) { if("zero" == word) b[i] = 0; if("one" == word) b[i] = 1; else if("two" == word) b[i] = 2; else if("three" == word) b[i] = 3; else if("four" == word) b[i] = 4; else if("five" == word) b[i] = 5; else if("six" == word) b[i] = 6; else if("seven" == word) b[i] = 7; else if("eight" == word) b[i] = 8; else if("nine" == word) b[i] = 9; else if("=" == word) break; i++; } if(10 == a[1]) first = a[0]; else first = a[0] * 10 + a[1]; if(10 == b[1]) second = b[0]; else second = b[0] * 10 + b[1]; sum = first + second; if(0 == sum) break; cout << sum << endl; } return 0; }