华为2013-08-14题目解答3
题目:输入一个字符串,转换为输出的数字,
字符串为英文字符,之间没有间隔
大写字母代表单词的头
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("");
char[] st = new char;
char[] u = stri.toCharArray();
int k = 1;
for (int j = 0; j < u.length; j++) {
if (((int) u <= (int) ('Z')) && ((int) u >= (int) ('A'))) {
st = u;
k++;
}
}
for (int i = 1; i < wordf.length; i++) {
wordf = st + wordf;
// System.out.println(wordf);
}
// translate
int f = wordf.length - 1;
int[] nums = new int;
int fflag = 0;
for (int i = 1; i < wordf.length; i++) {
if (wordf.equals("Zero")) {
nums = 0;
} else if (wordf.equals("O")) {
nums = 0;
} else if (wordf.equals("One")) {
nums = 1;
} else if (wordf.equals("Two")) {
nums = 2;
} else if (wordf.equals("Three")) {
nums = 3;
} else if (wordf.equals("Four")) {
nums = 4;
} else if (wordf.equals("Five")) {
nums = 5;
} else if (wordf.equals("Six")) {
nums = 6;
} else if (wordf.equals("Seven")) {
nums = 7;
} else if (wordf.equals("Eight")) {
nums = 8;
} else if (wordf.equals("Nine")) {
nums = 9;
}else if (wordf.equals("Double")) {
nums = 10;
}else{
nums = 11;
fflag = 1;
}
}
// check
for(int i = 0; i < nums.length; i++){
if(nums == 10){
if(nums<10){
nums = nums;
}else{
fflag = 1;
}
}
}
// number
if(fflag == 0){
for(int i = 0; i < nums.length; i++){
System.out.print(nums);
}
}else{
System.out.print(“ERROR”);
}
}
}
各种 if。。 来看看 谢谢分享,学习了 卧槽,华为的题目那么难 在cut段为何不直接用正则匹配分组来提取呢? vtianyun 发表于 2013-9-19 07:56
在cut段为何不直接用正则匹配分组来提取呢?
可以,我正则不太熟,所以只split一次 本帖最后由 jihteam 于 2013-9-19 13:18 编辑
昨天有人反映有bug
// check
for(int i = 0; i < nums.length; i++){
if(nums == 10){ if((i+1)<nums.length){
if(nums<10){
nums = nums;
}else{
fflag = 1;
}
}else{
fflag = 1;
}
}
}
页:
[1]