题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971
中文大意
将英文表示的数字翻译成阿拉伯数字。
核心思路
从前到后进行判断,遇到million ,thousand ,hundred乘以他们代表的值的大小。
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String[] a={"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven","twelve", "thirteen","fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "forty","fifty", "sixty", "seventy", "eighty","ninety"};
int[] b={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90};
while(sc.hasNext())
{
int t=sc.nextInt();
sc.nextLine();
while(t-->0)
{
String str=sc.nextLine();
String s[]=str.split(" ");
int x = 0;
int sum=0;
for(int i=0;i<s.length;i++)
{
if(s[i].equals("and"))
{
continue;
}
if(s[i].equals("million"))
{
sum*=1000000;
x+=sum;
sum=0;
}
else if(s[i].equals("thousand"))
{
sum*=1000;
x+=sum;
sum=0;
}
else if(s[i].equals("hundred"))
{
sum*=100;
}
else
{
for(int j=0;j<a.length;j++)
{
if(s[i].equals(a[j]))
{
sum+=b[j];
break;
}
}
}
}
x+=sum;
System.out.println(x);
}
}
}
}