读入两个小于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#实现(借助vs2005):
using System;
using System.Collections.Generic;
using System.Text;
namespace TestStringPlus
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
string strFirst = null;
string strLast = null;
strFirst=str.Substring(0,str.IndexOf('+'));
strLast = str.Substring(str.IndexOf('+')+2,str.IndexOf('=')-str.IndexOf('+')-2);
int first, last;
first = int.Parse(TransferToNum(strFirst).Trim());
last=int.Parse(TransferToNum(strLast).Trim());
if (first != 0 || last != 0)
Console.WriteLine(first+last);
}
static string TransferToNum(string s)
{
string strNum = null;
while (s.Length!=0)
{
strNum += GetNum(s.Substring(0,s.IndexOf(' ')));
s = s.Substring(s.IndexOf(' ')+1,s.Length-s.IndexOf(' ')-1);
}
return strNum;
}
static string GetNum(string s)
{
switch (s)
{
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";
default:
return null;
}
}
}
}
Java实现:
import java.io.*;
public class TestStringPlus{
public static void main(String args[]){
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
String strFirst = null;
String strLast = null;
strFirst=str.substring(0,str.indexOf('+'));
strLast = str.substring(str.indexOf('+')+2,str.indexOf('='));
int first, last;
first = Integer.parseInt(TransferToNum(strFirst).trim());
last=Integer.parseInt(TransferToNum(strLast).trim());
if (first != 0 || last != 0)
System.out.println(first+last);
}catch(IOException e){}
}
public static String TransferToNum(String s)
{
String strNum = "";
while (s.length()!=0)
{
strNum += GetNum(s.substring(0,s.indexOf(' ')));
s = s.substring(s.indexOf(' ')+1,s.length());
}
return strNum;
}
public static String GetNum(String s)
{
if(s.equals("zero"))
{
return "0";
}
else if(s.equals("one"))
{
return "1";
}
else if(s.equals("two"))
{
return "2";
}
else if(s.equals("three"))
{
return "3";
}
else if(s.equals("four"))
{
return "4";
}
else if(s.equals("five"))
{
return "5";
}
else if(s.equals("six"))
{
return "6";
}
else if(s.equals("seven"))
{
return "7";
}
else if(s.equals("eight"))
{
return "8";
}
else
{
return "9";
}
}
}
我同事写的代码(通过命令行参数,将英文数字存到数组当中来实现)
Java实现:
public static void main(String[] args)
{
boolean lable=true;
int signPosition=-1;
int left=0,right=0;
while(lable)
{
if(args.length==0)
{
System.err.println("你的输入不合法!请重新输入:");
}
else
{
for(int i=0;i<args.length;i++)
{
if(args[i].compareTo("+")==0)
{
signPosition=i;
break;
}
}
int temp=-1;
for(int i=0;i<args.length;i++)
{
if(args[i].compareTo("zero")==0)
temp=0;
else if(args[i].compareTo("one")==0)
temp=1;
else if(args[i].compareTo("two")==0)
temp=2;
else if(args[i].compareTo("three")==0)
temp=3;
else if(args[i].compareTo("four")==0)
temp=4;
else if(args[i].compareTo("five")==0)
temp=5;
else if(args[i].compareTo("six")==0)
temp=6;
else if(args[i].compareTo("seven")==0)
temp=7;
else if(args[i].compareTo("eight")==0)
temp=8;
else if(args[i].compareTo("nine")==0)
temp=9;
if(i<signPosition)
{
left+=temp*Math.pow(10, signPosition-i-1);
}
else if(i>signPosition)
{
right+=temp*Math.pow(10, args.length-i-2);
}
}
System.out.print(left+"+"+right+"="+(left+right));
}
lable=false;
break;
}