题目: 输入一个字符串,转换为输出的数字, 字符串为英文字符,之间没有间隔 大写字母代表单词的头 DoubleFive为55 FiveDouble输出错误 正确为输出数字,错误为输出ERROR。 import java.util.Scanner; import java.util.regex.Pattern; public class A5 { public static void main(String[] args) { // input Scanner s = new Scanner(System.in); String stri = s.next(); // cut String[] wordf = stri.split("[A-Z]"); char[] st = new char[30]; char[] u = stri.toCharArray(); int k = 1; for (int j = 0; j < u.length; j++) { if (((int) u[j] <= (int) ('Z')) && ((int) u[j] >= (int) ('A'))) { st[k] = u[j]; k++; } } for (int i = 1; i < wordf.length; i++) { wordf[i] = st[i] + wordf[i]; // System.out.println(wordf[i]); } // translate int f = wordf.length - 1; int[] nums = new int[f]; int fflag = 0; for (int i = 1; i < wordf.length; i++) { if (wordf[i].equals("Zero")) { nums[i - 1] = 0; } else if (wordf[i].equals("O")) { nums[i - 1] = 0; } else if (wordf[i].equals("One")) { nums[i - 1] = 1; } else if (wordf[i].equals("Two")) { nums[i - 1] = 2; } else if (wordf[i].equals("Three")) { nums[i - 1] = 3; } else if (wordf[i].equals("Four")) { nums[i - 1] = 4; } else if (wordf[i].equals("Five")) { nums[i - 1] = 5; } else if (wordf[i].equals("Six")) { nums[i - 1] = 6; } else if (wordf[i].equals("Seven")) { nums[i - 1] = 7; } else if (wordf[i].equals("Eight")) { nums[i - 1] = 8; } else if (wordf[i].equals("Nine")) { nums[i - 1] = 9; }else if (wordf[i].equals("Double")) { nums[i - 1] = 10; }else{ nums[i - 1] = 11; fflag = 1; } } // check for(int i = 0; i < nums.length; i++){ if(nums[i] == 10){ if(nums[i+1]<10){ nums[i] = nums[i+1]; }else{ fflag = 1; } } } // number if(fflag == 0){ for(int i = 0; i < nums.length; i++){ System.out.print(nums[i]); } }else{ System.out.print(“ERROR”); } } } |
[招聘|实习·全职·内推] 华为2013-08-14题目解答3
jihteam
· 发布于 2013-09-18 17:15
· 1605 次阅读
转载文章时务必注明原作者及原始链接,并注明「发表于 软院网 RuanYuan.Net 」,并不得对作品进行修改。