[招聘|实习·全职·内推] 华为2013-08-14题目解答3

jihteam · 发布于 2013-09-18 17:15 · 1605 次阅读
1265
题目:
输入一个字符串,转换为输出的数字,
字符串为英文字符,之间没有间隔
大写字母代表单词的头
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”);
                }
        }

}

共收到 7 条回复
xywhere · #2 · 2013-9-18 18:56:23  回复
各种 if。。
terry · #3 · 2013-9-18 19:11:27  回复
来看看
antty · #4 · 2013-9-18 19:54:02  回复 支持 反对
谢谢分享,学习了
magicliang · #5 · 2013-9-18 21:38:25  回复 支持 反对
卧槽,华为的题目那么难
vtianyun · #6 · 2013-9-19 07:56:52  回复 支持 反对
在cut段为何不直接用正则匹配分组来提取呢?

点评

可以,我正则不太熟,所以只split一次  详情 回复 发表于 2013-9-19 12:40
jihteam · #7 · 2013-9-19 12:40:55  回复 支持 反对
vtianyun 发表于 2013-9-19 07:56
在cut段为何不直接用正则匹配分组来提取呢?

可以,我正则不太熟,所以只split一次
jihteam · #8 · 2013-9-19 13:09:37  回复 支持 反对
本帖最后由 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[i+1]<10){
                                nums = nums[i+1];
                                }else{
                                        fflag = 1;
                                }
                        }else{
                                       fflag = 1;
                        }
                        }
                }


回帖
B Color Image Link Quote Code Smilies
Command + Enter
快速回复 返回顶部 返回列表