1295 爱翻译
Time Limit : 2000/1000 MS(Java/Others) | Memory Limit :65536/32768 KB(Java/Others)
Submits : 390 | Solved : 80
Description
英语是现在世界第一大语言,所以学好英语是very important, 但是宁波大学的某个大牛说:“我不怕,我可以写一个程序让各种语言 快速转换。”哎呀,学编程的,惹不起...溜...┈━═☆ 咱不是大牛,但有追逐大牛的梦想,大牛的伟大程序不是随便能盗版出来的, 我们就做一个简单的翻译器,把阿拉伯数字转换为英文输出。
Input
小于10000的非负阿拉伯数字整数n。
Output
对应的英文,如样例,一个一行。
Sample Input
3923 5021
Sample Output
three thousand nine hundred twenty three five thousand twenty one
HINT
Source
我写的代码已知PE,找不到哪儿格式错误
package com.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class test1 {
private static Scanner sc;
public static void main(String[] args){
sc=new Scanner(System.in);
Map<Integer, String> map=new HashMap<Integer, String>();
map.put(0,"zero ");
map.put(1,"one ");
map.put(2,"two ");
map.put(3,"three ");
map.put(4,"four ");
map.put(5,"five ");
map.put(6,"six ");
map.put(7,"seven ");
map.put(8,"eight ");
map.put(9,"nine ");
map.put(10, "ten ");
map.put(11, "eleven ");
map.put(12, "twelve ");
map.put(13, "thirteen ");
map.put(14, "fourteen ");
map.put(15, "fifteen ");
map.put(16,"sixteen ");
map.put(17,"seventeen ");
map.put(18,"eighteen ");
map.put(19,"nineteen ");
map.put(20,"twenty ");
map.put(30,"thirty ");
map.put(40,"forty ");
map.put(50,"fifty ");
map.put(60,"sixty ");
map.put(70,"seventy ");
map.put(80,"eighty ");
map.put(90,"ninety ");
map.put(100,"hundred ");
map.put(1000,"thousand ");
while(sc.hasNext()){
int num=sc.nextInt();
String str="";
if(num/1000>0){
str=map.get(num/1000).toString()+map.get(1000).toString();
num=num%1000;
}
if(num/100>0){
str+=map.get(num/100).toString()+map.get(100).toString();
num%=100;
}
if(num/10>0){
if(num/10==1){
str+=map.get(num).toString();
num=0;
}
else{
int l=num/10*10;
str+=map.get(l).toString();
}
num%=10;
}
if(num>0){
str+=map.get(num).toString();
}
if(num==0&&str.equals("")){
str+=map.get(0).toString();
}
if(!str.equals(""))
System.out.println(str);
}
}
}
朋友AC的代码
import java.util.Scanner;
public class Main{
static String[] ans = new String[30];
static int cnt = 0;
public static void extend_a(char key) {
switch(key) {
case '1': ans[cnt++]= "one"; break;
case '2': ans[cnt++]= "two"; break;
case '3': ans[cnt++]= "three"; break;
case '4': ans[cnt++]= "four"; break;
case '5': ans[cnt++]= "five"; break;
case '6': ans[cnt++]= "six"; break;
case '7': ans[cnt++]= "seven"; break;
case '8': ans[cnt++]= "eight"; break;
case '9': ans[cnt++]= "nine"; break;
}
}
public static void len_one(String key){
char ch = key.charAt(0);
if(ch != '0') {
extend_a(ch);
}
}
public static void len_two(String key){
char ch = key.charAt(0);
if(ch != '0') {
if(ch == '1') {
switch(key) {
case "10": ans[cnt++]= "ten"; break;
case "11": ans[cnt++]= "eleven"; break;
case "12": ans[cnt++]= "twelve"; break;
case "13": ans[cnt++]= "thirteen"; break;
case "14": ans[cnt++]= "fourteen"; break;
case "15": ans[cnt++]= "fifteen"; break;
case "16": ans[cnt++]= "sixteen"; break;
case "17": ans[cnt++]= "seventeen"; break;
case "18": ans[cnt++]= "eighteen"; break;
case "19": ans[cnt++]= "nineteen"; break;
}
return;
} else{
switch(ch) {
case '2': ans[cnt++]= "twenty"; break;
case '3': ans[cnt++]= "thirty"; break;
case '4': ans[cnt++]= "forty"; break;
case '5': ans[cnt++]= "fifty"; break;
case '6': ans[cnt++]= "sixty"; break;
case '7': ans[cnt++]= "seventy"; break;
case '8': ans[cnt++]= "eighty"; break;
case '9': ans[cnt++]= "ninety"; break;
}
}
}
String temp = "" + key.charAt(1);
len_one(temp);
}
public static void len_three(String key){
if(key.charAt(0) != '0') {
extend_a(key.charAt(0));
ans[cnt++] = "hundred";
}
String temp = "" + key.charAt(1) + key.charAt(2);
len_two(temp);
}
public static void len_four(String key){
extend_a(key.charAt(0));
ans[cnt++] = "thousand";
String temp = ""+key.charAt(1) + key.charAt(2) + key.charAt(3);
len_three(temp);
}
public static void main(String[] args) throws Exception{
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String num = in.next();
cnt = 0;
switch(num.length()) {
case 1: len_one(num); break;
case 2: len_two(num); break;
case 3: len_three(num); break;
case 4: len_four(num); break;
}
for(int i = 0; i < cnt; ++i) {
if(i == 0)System.out.print(ans[i]);
else System.out.print(" " + ans[i]);
}
System.out.println();
}
in.close();
}
}