测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
对每个测试用例输出1行,即A+B的值.
one + two = three four + five six = zero seven + eight nine = zero + zero =
3 90 96
#include<stdio.h>//1.从加号分开a和b, 2.根据是否有空格判断是几位数 3.把字母根据数组的下标转换成数字
int main()
{
char N[100];int i,j,num,a=0,b=0,n;
gets(N);n=strlen(N);
char number[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
//先找到加号的位置
for(i=0;N[i]!='\0';i++)
if(N[i]=='+')
{num=i;break;}
//操作a
N[num-1]='\0';int key=1;char t[10],t1[10];//从加号分割,加号前面的空格改成\0
for(i=0;i<num-1;i++)
if(N[i]==' ')//证明由两个数组成
{
key=2;
N[i]='\0';
break;
}
if(key==2)
{
strcpy(t,N);
strcpy(t1,N+i+1);
for(j=0;j<10;j++)//转换成数字
{
if(strcmp(t,number[j])==0)
a+=j*10;
if(strcmp(t1,number[j])==0)
a+=j;
}
}
else{//一个数
strcpy(t,N);
for(j=0;j<10;j++)
if(strcmp(t,number[j])==0)
a+=j;
}
//操作b
N[n-2]='\0';//去掉=以及等号前面的空格
key=1;
for(i=num+2;i<n-2;i++)
if(N[i]==' ')//证明由两个数组成
{
key=2;
N[i]='\0';
break;
}
if(key==2)
{
strcpy(t,N+num+2);//别忘记起始的位置
strcpy(t1,N+i+1);
for(j=0;j<10;j++)//转换成数字
{
if(strcmp(t,number[j])==0)
b+=j*10;
if(strcmp(t1,number[j])==0)
b+=j;
}
}
else{//一个数
strcpy(t,N+num+2);
for(j=0;j<10;j++)
if(strcmp(t,number[j])==0)
b+=j;
}
printf("%d\n",a+b);
} #include<iostream>
(720)#include<string>
#include<cstring>
(803)#include<map>
using namespace std;
int main(){
//预处理
map<string,int> mp;
mp["one"] = 1;
mp["two"] = 2;
mp["three"] = 3;
mp["four"] = 4;
mp["five"] = 5;
mp["six"] = 6;
mp["seven"] = 7;
mp["eight"] = 8;
mp["nine"] = 9;
mp["zero"] = 0;
int num[2] = {0};
string str;
int cnt = 0;
while(cin >> str){
if(str[0]=='+'){
cnt ++;
}else if(str[0]=='='){
if(num[0]==0 && num[1]==0) {
return 0;
}
printf("%d\n",num[0]+num[1]);
cnt = 0; //还原
memset(num,0,sizeof(num));
}else {
num[cnt] = num[cnt] * 10 + mp[str];
}
}
return 0;
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String line = scanner.nextLine();
String s = line.replace("zero", "0")
.replace("one", "1")
.replace("two", "2")
.replace("three", "3")
.replace("four", "4")
.replace("five", "5")
.replace("six", "6")
.replace("seven", "7")
.replace("eight", "8")
.replace("nine", "9")
.replace(" ", "")
.replace("=", "");
String[] ss = s.split("\\+");
System.out.println(Integer.parseInt(ss[0])+Integer.parseInt(ss[1]));
}
}
} #include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int changeStrToInt(string s){
int res;
string str[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
for(int i=0;i<10;i++){
if(s.compare(str[i])==0){
res=i;
break;
}
}
return res;
}
int getStrToNumber(string s){
int res;
if(s.find(' ')==-1) res=changeStrToInt(s);
else if(s.find(' ')>0){
int first=changeStrToInt(s.substr(0,s.find(' ')));
int second=changeStrToInt(s.substr(s.find(' ')+1,s.length()-s.find(' ')));
res=first*10+second;
}
return res;
}
int main(){
string s;
vector<int> v;
while(getline(cin,s)){
int plusloc=s.find('+'),equalloc=s.find('=');
string first_str=s.substr(0,plusloc-1);
string second_str=s.substr(plusloc+2,equalloc-plusloc-3);
if(changeStrToInt(first_str)==0&&changeStrToInt(second_str)==0) break;
else{
v.push_back(getStrToNumber(first_str)+getStrToNumber(second_str));
}
}
for(int i=0;i<v.size();i++){
cout<<v[i]<<endl;
}
return 0;
}
while True:
try:
string = input().lower()
if string == "zero + zero =":
break
numEnglish = ['zero','one','two','three','four','five','six','seven','eight','nine']
string = string.split("+")
left = string[0].strip().split()
right = string[1].strip('=').split()
leftNum = '0'
rightNum = '0'
for i in left:
leftNum += str(numEnglish.index(i))
for i in right:
rightNum += str(numEnglish.index(i))
print(int(leftNum)+int(rightNum))
except Exception:
break
//C++也就是分割字符,substr
//我相信python更适合此类题目
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string,int>mp;
int main()
{
mp["zero"]=0;
mp["one"]=1;
mp["two"]=2;
mp["three"]=3;
mp["four"]=4;
mp["five"]=5;
mp["six"]=6;
mp["seven"]=7;
mp["eight"]=8;
mp["nine"]=9;
string a;
while(getline(cin,a))
{
int x,y;
int pos1=a.find('+');
int pos2=a.find('=');
string str1=a.substr(0,pos1-1);
string str2=a.substr(pos1+2,pos2-pos1-3);
if(str1.find(' ')==string::npos)
x=mp[str1];
else
{
int pos3=str1.find(' ');
string s1=str1.substr(0,pos3);
string s2=str1.substr(pos3+1);
x=mp[s1]*10+mp[s2];
}
if(str2.find(' ')==string::npos)
y=mp[str2];
else
{
int pos3=str2.find(' ');
string s1=str2.substr(0,pos3);
string s2=str2.substr(pos3+1);
y=mp[s1]*10+mp[s2];
}
if(x+y==0)
break;
else
cout<<x+y<<endl;
}
return 0;
}
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string,int>mp;
mp["zero"]=0;
mp["one"]=1;
mp["two"]=2;
mp["three"]=3;
mp["four"]=4;
mp["five"]=5;
mp["six"]=6;
mp["seven"]=7;
mp["eight"]=8;
mp["nine"]=9;
string s;
while(cin>>s){
int a1,a2=0;
a1 = mp[s];
bool flag = false;//用于标记是否进入第二个加数的输入
while(true){
string temp;
cin>>temp;
if(temp=="+") {
flag =true;
}else if(temp=="="){
break;
}else{
if(flag){//如果flag = true,说明已经进入第二个加数
a2=a2*10 + mp[temp];
}else{//如果flag = false,说明还在输入第一个加数
a1=a1*10 + mp[temp]; }
}
}
if(a1==0 && a2==0) break;
else cout<<a1+a2<<endl;
}
return 0;
}
//加号只可能出现在第二位或第三位。一共有4中可能:1位+1位,1位+2位,2位+1位,2位+2位。分别罗列下就好,不用分割字符串。
#include<iostream>
#include<string>
using namespace std;
int getNum(string str){
if(str=="one"){return 1;}
else if(str=="two"){return 2;}
else if(str=="three"){return 3;}
else if(str=="four"){return 4;}
else if(str=="five"){return 5;}
else if(str=="six"){return 6;}
else if(str=="seven"){return 7;}
else if(str=="eight"){return 8;}
else if(str=="nine"){return 9;}
else if(str=="zero"){return 0;}
else {return -1;}
}
int main(){
string str1,str2,str3,str4,str5;
int a,b,c,d,e,ans,t,num1,num2;
while(cin>>str1>>str2>>str3){//前三位一定是数字或加号。
if(str1=="zero"&&str2=="zero"){break;}
a=getNum(str1);
b=getNum(str2);
c=getNum(str3);
if(b==-1){
num1=a;
cin>>str4;
if(str4=="="){num2=c;}//1位+1位
else{
num2=c*10+getNum(str4);//1位+2位
}
}
else{
num1=a*10+b;
cin>>str4>>str5;
if(str5=="="){num2=getNum(str4);}//2位+1位
else{num2=getNum(str4)*10+getNum(str5);}//2位+2位
}
cout<<num1+num2<<endl;
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char a[50],ans[10];
char *s;
int i,j,sum,p;
while(gets(a))
{
s=strtok(a," ");
j=1,sum=0,p=0; //用j存储当前是个位还是十位
do
{
if(strcmp(s,"zero")==0){ans[p++]=0;continue;}
if(strcmp(s,"one")==0){ans[p++]=1;continue;}
if(strcmp(s,"two")==0){ans[p++]=2;continue;}
if(strcmp(s,"three")==0){ans[p++]=3;continue;}
if(strcmp(s,"four")==0){ans[p++]=4;continue;}
if(strcmp(s,"five")==0){ans[p++]=5;continue;}
if(strcmp(s,"six")==0){ans[p++]=6;continue;}
if(strcmp(s,"seven")==0){ans[p++]=7;continue;}
if(strcmp(s,"eight")==0){ans[p++]=8;continue;}
if(strcmp(s,"nine")==0){ans[p++]=9;continue;}
if(strcmp(s,"+")==0){ans[p++]=-1;continue;}
}while(s=strtok(NULL," "));
if(ans[0]==0 && ans[2]==0)break;
for(i=p-1;i>=0;i--)
{
if(ans[i]!=-1)
{
sum+=ans[i]*j;
j=j*10;
}
else j=1; //若此时遇到加号(-1),重置j
}
printf("%d\n",sum);
}
}
#include<bits/stdc++.h>
using namespace std;
int main() {
string S,A,B;
int a,b;
map<string,char>mp;
stringstream ss;
mp["zero"]='0';
mp["one"]='1';
mp["two"]='2';
mp["three"]='3';
mp["four"]='4';
mp["five"]='5';
mp["six"]='6';
mp["seven"]='7';
mp["eight"]='8';
mp["nine"]='9';
while(1) {
a=0,b=0;
A="";
B="";
while(cin>>S) {
if(S=="+") break;
A+=mp[S];
}
ss<<A;
ss>>a;
ss.clear();
while(cin>>S) {
if(S=="=") break;
B+=mp[S];
}
ss<<B;
ss>>b;
ss.clear();
if(a==0&&b==0) break;
cout<<a+b<<endl;
}
return 0;
} 这个应该是比较简单的代码!
// 老夫就是api一把梭!时间复杂度不存在的
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] num = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
while (sc.hasNextLine()) {
String str = sc.nextLine();
String pre = str.substring(0, str.indexOf("+") - 1);
String last = str.substring(str.indexOf("+") + 2, str.length() - 2);
int a = 0;
int b = 0;
//a
String[] list = pre.split(" ");
if (list.length == 1) {
for (int i = 0; i < num.length; i++) {
if (num[i].equals(pre)) {
a = i;
break;
}
}
} else {
for (int i = 0; i < num.length; i++) {
if (num[i].equals(list[0])) {
a = i * 10;
}
}
for (int i = 0; i < num.length; i++) {
if (num[i].equals(list[1])) {
a += i;
}
}
}
//b
String[] list2 = last.split(" ");
if (list2.length == 1) {
for (int i = 0; i < num.length; i++) {
if (num[i].equals(last)) {
b = i;
break;
}
}
} else {
for (int i = 0; i < num.length; i++) {
if (num[i].equals(list2[0])) {
b = i * 10;
}
}
for (int i = 0; i < num.length; i++) {
if (num[i].equals(list2[1])) {
b += i;
}
}
}
if (a == 0 && b == 0)
return;
System.out.println(a + b);
}
}
} #include <iostream>
#include <map>
using namespace std;
map<string, int> str2int = { {"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9} };
int main()
{
string str_op;
int a = 0, b = 0, tmp = 0;
while (cin>>str_op)
{
if ("+" == str_op)
{
a = tmp; tmp = 0;
}
else if ("=" == str_op)
{
b = tmp; tmp = 0;
if (0 == a && 0 == b) break;
cout << a + b << endl;
}
else tmp = (tmp * 10 + str2int[str_op]);
}
return 0;
} #include <iostream>
#include <sstream>
#include "vector"
using namespace std;
int change(string str){//把短字符串变成数字
if(str=="one") return 1;
else if(str=="two") return 2;
else if(str=="three") return 3;
else if(str=="four") return 4;
else if(str=="five") return 5;
else if(str=="six") return 6;
else if(str=="seven") return 7;
else if(str=="eight") return 8;
else if(str=="nine") return 9;
else return 0;
}
int fun(string str) {//把长字符串变成数字
stringstream ss(str);
vector<string> tokens; // 存储分隔后的子字符串
string token;
while (ss >> token) {
tokens.push_back(token);
}
int num=0;
for(auto & token : tokens){
num= num*10 + change(token);
}
return num;
}
int main() {
string str;
while (getline(cin, str)) {
int pos_add, pos_equal;//'+'的位置和'='的位置
string qian = "", hou = "";//'+'前面的数字和'+'后面的数字
for (int i = 0; i < str.size(); i++) {
if (str[i] == '+') {
pos_add = i;
}
if (str[i] == '=') {
pos_equal = i;
}
}
for (int i = 0; i < pos_add - 1; i++) {
qian += str[i];
}
for (int i = pos_add + 2; i < pos_equal - 1; i++) {
hou += str[i];
}
if(fun(qian)+fun(hou)!=0)
cout<<fun(qian)+fun(hou)<<endl;
}
}
#include <bits/stdc++.h>
using namespace std;
int num(string s) {
if (s == "zero") return 0;
if (s == "one") return 1;
if (s == "two") return 2;
if (s == "three") return 3;
if (s == "four") return 4;
if (s == "five") return 5;
if (s == "six") return 6;
if (s == "seven") return 7;
if (s == "eight") return 8;
if (s == "nine") return 9;
return 0;
}
int main() {
string s;
while (getline(cin, s)) {
string tmp1, tmp2;
int num1 = 0, num2 = 0;
int flg = 1;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '+') {
flg = 2;
continue;
} else if (s[i] == '=') {
break;
} else if (s[i] == ' ' and flg == 1 and tmp1 != "") {
num1 *= 10;
num1 += num(tmp1);
tmp1 = "";
} else if (s[i] == ' ' and flg == 2 and tmp2 != "") {
num2 *= 10;
num2 += num(tmp2);
tmp2 = "";
} else if(s[i]!=' '){
if (flg == 1) tmp1 += s[i];
else tmp2 += s[i];
}
}
if (num1 == 0 and num2 == 0) break;
else cout << num1 + num2 << endl;
}
} #include <bits/stdc++.h>
using namespace std;
int main() {
map<string,int>m;
m["zero"]=0;
m["one"]=1;
m["two"]=2;
m["three"]=3;
m["four"]=4;
m["five"]=5;
m["six"]=6;
m["seven"]=7;
m["eight"]=8;
m["nine"]=9;
string s;
int sum1=0;
int sum2=0;
bool flag=false;
bool num=false;
while(cin>>s){
if(s!="+"&&s!="="&&!num){
if(flag==false)
sum1+=m[s];
else
sum1=sum1*10+m[s];
flag=true;
}
else if(s=="+"){
num=true;
flag=false;
}
else if(s!="+"&&s!="="&&num){
if(flag==false)
sum2+=m[s];
else
sum2=sum2*10+m[s];
flag=true;
}
}
cout<<sum1+sum2;
}
// 64 位输出请用 printf("%lld") def fun(a, l): larr = [] rarr = [] c=0 k=0 m = int(l/2) op = a[m] for i in a: c+=1 if c > m: rarr.append(i) elif c<m: larr.append(i) dig = ['zero','one' , 'two' ,'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'] for i in range(len(rarr)): for k in range(len(dig)): if i < len(rarr): if rarr[i] == dig[k]: rarr[i] = k if i < len(larr): if larr[i] == dig[k]: larr[i] = k #prit() return rarr, larr def fun2(a, b): m = len(a)-1 m1 = len(b)-1 e=0 f = 0 for i in a: if m < 0: break e += i*(10**m) m-=1 for i in b: if m1 < 0: break f += i*(10**m1) m1-=1 return e, f def add(a, l): r, l = fun(a, l) r.pop() res1, res2=fun2(r, l) res = res1+res2 print(res) while True: try: s = input().split() if s[0] == 'zero' and s[2] == 'zero': break a = len(s) add(s, a) except: break
#include<stdio.h>
#include<map>
#include<string>
#include<iostream>
using namespace std;
int add(string str1,string str2){
map<string,int> stoi={{"zero",0},{"one",1},{"two",2},
{"three",3},{"four",4},{"five",5},
{"six",6},{"seven",7},{"eight",8},{"nine",9}};
int s1,g1,s2,g2,pos1,pos2,op1,op2;
string ss1,sg1,ss2,sg2;
pos1=str1.find(' ');
pos2=str2.find(' ');
if(pos1==-1){
g1=stoi[str1];
s1=0;
}else{
ss1=str1.substr(0,pos1);
sg1=str1.substr(pos1+1);
s1=stoi[ss1];
g1=stoi[sg1];
}
op1=s1*10+g1;
if(pos2==-1){
g2=stoi[str2];
s2=0;
}else{
ss2=str2.substr(0,pos2);
sg2=str2.substr(pos2+1);
s2=stoi[ss2];
g2=stoi[sg2];
}
op2=s2*10+g2;
return op1+op2;
}
int main(){
char op1[101],op2[101];
string str1,str2,str;
while(getline(cin,str)){
if(str.size()==0) break;
int pos=str.find('+');
str1=str.substr(0,pos-1);
int ed=str.size()-3;
str2=str.substr(pos+2,ed-pos-1);
if(str1=="zero"&&str2=="zero") break;
// printf("%s\n",str1.c_str());
// printf("%s\n",str2.c_str());
printf("%d\n",add(str1,str2));
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashMap<String,Integer> dic = new HashMap<>();
dic.put("zero", 0);
dic.put("one", 1);
dic.put("two", 2);
dic.put("three", 3);
dic.put("four", 4);
dic.put("five", 5);
dic.put("six", 6);
dic.put("seven", 7);
dic.put("eight", 8);
dic.put("nine", 9);
while (in.hasNextLine()) {
String s = in.nextLine();
if(s.equals("zero + zero ="))
break;
String[] strs = s.split(" ");
int index = 0;
int a = 0;
while(!strs[index].equals("+")){
a *= 10;
a += dic.get(strs[index]);
index++;
}
index++;
int b = 0;
while(!strs[index].equals("=")){
b *= 10;
b += dic.get(strs[index]);
index++;
}
System.out.println(a + b);
}
}
} 手工制表可还行