日期:2014-05-20  浏览次数:20751 次

笔试题 讨论一下
昨天晚上参加baidu笔试的2个题目,大家讨论一下:

1.序列Seq=[a,b,…z,aa,ab…az,ba,bb,…bz,…,za,zb,…zz,aaa,…] 类似与excel的排列,任意给出一个字符串s=[a-z]+(由a-z字符组成的任意长度字符串),请问s是序列Seq的第几个。

2.需求:需要引入用户对搜索结果相关性的评分100分制,希望用户的打分能帮助搜索引擎排序,但又要避免恶意投票,作弊等,请设计一个比较公平的评分系统。



------解决方案--------------------
第一题,相当于26进制嘛,
------解决方案--------------------
Java code
public class Test1 {
    
    public static void main(String[] args) {
        int n = letter2Number("abc");
        System.out.println(n);
    }
    
    public static int letter2Number(String letters) {
        if(!letters.matches("[a-zA-Z]+")) {
            throw new IllegalArgumentException("Format ERROR!");
        }
        char[] chs = letters.toLowerCase().toCharArray(); 
        int result = 0;
        for(int i = chs.length - 1, p = 1; i >= 0; i--) {            
            result += getNum(chs[i]) * p;
            p *= 26;
        }
        return result;
    }
    
    private static int getNum(char c) {
        return c - 'a' + 1;
    }
}

------解决方案--------------------
Java code

String str = "abcdefghijklmnopqrstuvwxyz"; 
    public int getNum(String oneChar){//传一个字母 
        return str.indexOf(oneChar)+1; 
    } 
    public int q26(int a){
        if(a==0){
            return 1;
        }
        else if(a==1){
            return 26;
        }
        else{
            return 26*q26(a-1);
        }
    }
    public int changeToNum(String moreChar){//传的是一个字符串 
        int all = 0; 
        int p = 0;
        char[] newOne = moreChar.toCharArray(); 
        //挨个字母换成十进制数,然后再运算 
        for(int i = 0 ;i <newOne.length ; i++){ 
            String tag = Character.toString(newOne[i]);
            int num = getNum(tag);
            all = all + num*q26(newOne.length-i-1);
        } 
        return all; 
    }

------解决方案--------------------
那么麻烦?

JDK里面就有了,一句话:

ava.lang.Integer.parseInt(String "abc", 26);



int java.lang.Integer.parseInt(String s, int radix) throws NumberFormatException
Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether java.lang.Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned. 

An exception of type NumberFormatException is thrown if any of the following situations occurs: 

The first argument is null or is a string of length zero. 
The radix is either smaller than java.lang.Character.MIN_RADIX or larger than java.lang.Character.MAX_RADIX. 
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1. 
The value represented by the string is not a value of type int. 
Examples: 

 parseInt("0", 10) returns 0
 parseInt("473", 10) returns 473
 parseInt("-0", 10) returns 0
 parseInt("-FF", 16) returns -255
 parseInt("1100110", 2) returns 102
 parseInt("2147483647", 10) returns 2147483647
 parseInt("-2147483648", 10) returns -2147483648
 parseInt("2147483648", 10) throws a NumberFormatException
 parseInt("99", 8) throws a NumberFormatException
 parseInt("Kona", 10) throws a NumberFormatException
 parseInt("Kona", 27) returns 411787