1381 - A + B
时间限制 : 1 秒
内存限制 : 32 MB
读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.
题目输入
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
题目输出
对每个测试用例输出1行,即A+B的值.
输入/输出样例
输入格式
one + two = three four + five six = zero seven + eight nine = zero + zero =
输出格式
3 90 96
C语言解答
#include "stdio.h" #include "string.h" char strnum[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; int getnum(char str[]){ int i; for(i=0;i<10;i++){ if(!strcmp(strnum[i],str))//strcmp两字符串相等是为0 return i; } return 0; } int main() { char str[100]; while(scanf("%s",str)!=EOF){ int a=0; int b=0; while(strcmp(str,"+")){ a=a*10+getnum(str); scanf("%s",str);} while(strcmp(str,"=")){ b=b*10+getnum(str); scanf("%s",str);} if(!a&&!b) break; printf("%d\n",a+b); } return 0; }
C++解答
#include <stdio.h> #include <string.h> char strNum[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; int GetNum(char str[]){ for(int i=0; i<10; i++){ if(!strcmp(str, strNum[i])){ return i; } } return 0; } int main(){ char str[100]; while(scanf("%s", str) != EOF){ int a = 0; int b = 0; while(strcmp(str, "+")){ a = a*10 + GetNum(str); scanf("%s", str); } while(strcmp(str, "=")){ b = b*10 + GetNum(str); scanf("%s", str); } if(!a && !b){ break; } printf("%d\n", a+b); } return 0; }
Java解答
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while(scanner.hasNext()) { String temp=scanner.nextLine(); calculate(temp); } } private static void calculate(String temp) { String buffer[]=temp.split(" "); if(buffer.length==4) { int r=translate(buffer[0])+translate(buffer[2]); if(r!=0) System.out.println(r); } else if (buffer.length==6) { System.out.println(translate(buffer[0])*10+translate(buffer[1])+translate(buffer[3])*10+translate(buffer[4])); } else if (buffer.length==5) { if(buffer[2].equals("+")) { System.out.println(translate(buffer[0])*10+translate(buffer[1])+translate(buffer[3])); } else if (buffer[1].equals("+")) { System.out.println(translate(buffer[0])+translate(buffer[2])*10+translate(buffer[3])); } } } private static int translate(String string) { switch (string) { case "zero": return 0; case "one": return 1; case "two": return 2; case "three": return 3; case "four": return 4; case "five": return 5; case "six": return 6; case "seven": return 7; case "eight": return 8; case "nine": return 9; } return -1; } }
Python解答
#!/usr/bin/env python # -- coding: utf-8 -- num_lists = { 'zero' : 0, 'one' : 1, 'two' : 2, 'three' : 3, 'four' : 4, 'five' : 5, 'six' : 6, 'seven' : 7, 'eight' : 8, 'nine' : 9, } iszero = False # 当 zero + zero 时,停止 while not iszero: line = raw_input() chars = line.split() if chars[1] == '+' and chars[3] == '=': a = num_lists[chars[0]] b = num_lists[chars[2]] elif chars[1] == '+' and chars[4] == '=': a = num_lists[chars[0]] b = num_lists[chars[2]] * 10 + num_lists[chars[3]] elif chars[2] == '+' and chars[4] == '=': a = num_lists[chars[0]] * 10 + num_lists[chars[1]] b = num_lists[chars[3]] elif chars[2] == '+' and chars[5] == '=': a = num_lists[chars[0]] * 10 + num_lists[chars[1]] b = num_lists[chars[3]] * 10 + num_lists[chars[4]] else: print "input error!" if chars[0] == 'zero' and chars[2] == 'zero': iszero = True else: print a + b