题目1010:A + B
- 题目描述:
-
读入两个小于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
1 import java.util.Hashtable; 2 import java.util.Scanner; 3 4 public class Main{ 5 public static void main(String[]args){ 6 Scanner in=new Scanner(System.in); 7 Hashtable<String,Integer> ht=new Hashtable(); 8 initHashtable(ht); 9 while(in.hasNext()){ 10 String input=in.nextLine(); 11 String[] t=input.split(" "); 12 int len=t.length; 13 int cout=0; 14 String[]num=new String[2]; 15 num[0]=""; 16 num[1]="";//一定要初始化为空串,不然加上一个数字,比如1,就会变成null1; 17 for(int i=0;i<len;i++){ 18 if(t[i].equals("+")){ 19 cout++; 20 continue; 21 } 22 else if(t[i].equals("=")){ 23 continue; 24 } 25 int a=ht.get(t[i]); 26 num[cout]=num[cout]+a; 27 } 28 int x=Integer.parseInt(num[0]); 29 int y=Integer.parseInt(num[1]); 30 if(x==0&&y==0){ 31 break; 32 } 33 System.out.println(x+y); 34 } 35 } 36 37 public static void initHashtable(Hashtable<String,Integer> ht){ 38 ht.put("zero",0); 39 ht.put("one",1); 40 ht.put("two",2); 41 ht.put("three",3); 42 ht.put("four",4); 43 ht.put("five",5); 44 ht.put("six",6); 45 ht.put("seven",7); 46 ht.put("eight",8); 47 ht.put("nine",9); 48 ht.put("tene",10); 49 } 50 } 51 /************************************************************** 52 Problem: 1010 53 User: 0000H 54 Language: Java 55 Result: Accepted 56 Time:80 ms 57 Memory:15548 kb 58 ****************************************************************/