首页 > 专题系列 > Java解POJ > POJ 2121 Inglish-Number Translator [解题报告] Java
2013
11-10

POJ 2121 Inglish-Number Translator [解题报告] Java

Inglish-Number Translator

问题描述 :

In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English words that your program must account for:

negative, 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, hundred, thousand, million

输入:

The input consists of several instances. Notes on input:
  1. Negative numbers will be preceded by the word negative.
  2. The word “hundred” is not used when “thousand” could be. For example, 1500 is written “one thousand five hundred”, not “fifteen hundred”.

The input is terminated by an empty line.

输出:

The answers are expected to be on separate lines with a newline after each.

样例输入:

six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two

样例输出:

6
-729
1000101
814022

解题代码:

//* @author: [email protected]
import java.io.*;
import java.util.*;
public class Main
{
 static String[] Num=new String[]{
	"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",
	"hundred","thousand","million"
  };

  public static void main(String[] args) throws IOException
  {
   InputStreamReader is=new InputStreamReader(System.in);
   BufferedReader in=new BufferedReader(is);
   String s;
   while(true)
    {
      s=in.readLine();
      if(s.equals("")) break;
      boolean bb=true;
      String[] ss=s.split(" ");
      int len=ss.length;
      long sum=0;
      long temp=0;
      int tag=0;
      if(ss[0].equals("negative")){
	 bb=false;
	 tag=1;
      }
      for(int i=tag;i< len;i++)
      {
	 for(int j=0;j< 31;j++)
	 {
	    if(ss[i].equals(Num[j]))
	    {
		 if(j<=20) temp+=j;
		 else if(j< 28) temp+=(j-18)*10;
		 else if(j==28) temp*=100;
		 else if(j==29)
		 {
	           temp*=1000;sum+=temp;temp=0;
		 }
		 else if(j==30)
		 {
		   sum+=temp;sum*=1000000;temp=0;
		 }
		 break;
	     }
	  }
       }
       sum+=temp;
       if(!bb) sum=-sum;
       System.out.println(sum);
    }
  }
}

  1. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同,其树的前、中、后序遍历是相同的,但在此处不能使用中序遍历,因为,中序遍历的结果就是排序的结果。经在九度测试,运行时间90ms,比楼主的要快。

  2. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }