面试题(一)

第一题:某个公司举行一场羽毛球赛,有1001个人参加,现在为了评比出“最厉害的那个人”,进行淘汰赛,请问至少需要进行多少次比赛。

public int calculateGames(int numPerson, int alreadyGames) {
        int extra = numPerson % 2;
        int thisRoundGames = numPerson / 2;
        if ((extra + thisRoundGames) == 1) {
            return alreadyGames + thisRoundGames;
        } else {
            return calculateGames(thisRoundGames + extra, thisRoundGames
                    + alreadyGames);
        }
    }



第二题:一百个灯泡排成一排,第一轮将所有灯泡打开;第二轮每隔一个灯泡关掉一个。即排在偶数的灯泡被关掉,第三轮每隔两个灯泡,将开着的灯泡关掉,关掉的灯泡打开。依次类推,第n轮结束的时候,还有几盏灯泡亮着。

public class AlgorithmA {

    private int lights[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1 };

    public void calculateLights(int round) {
        int numLights = lights.length;
        for (int i = 1; i < round; i++) {
            int x = i;
            if (x > numLights) {
                break;
            }
            for (int num = 0; num < numLights; num++) {
                if (x == 0) {
                    if (lights[num] == 1) {
                        lights[num] = 0;
                    } else {
                        lights[num] = 1;
                    }
                    x = i;
                } else {
                    x--;
                }
            }
            printLights();
        }
    }

    private void printLights() {
        int numLights = lights.length;
        for (int num = 0; num < numLights; num++) {
            System.out.print(lights[num] + " ");
        }
        System.out.println(" ");
    }

    public static void main(String args[]) {
        AlgorithmA algorithmA = new AlgorithmA();
        algorithmA.calculateLights(6);
    }
}



第三题 Bubble Sort Algorithm


package algorithm;

public class BubbleSort {

    public int[] bubbleSort(int[] numbers) {
        int length = numbers.length - 1;
        int z = 0;
        for (int x = 0; x < length; x++) {
            for (int y = 0; y < (length - x); y++) {
                if (numbers[y] > numbers[y + 1]) {
                    z = numbers[y];
                    numbers[y] = numbers[y + 1];
                    numbers[y + 1] = z;
                }
            }
        }
        return numbers;
    }

    public static void main(String args[]) {
        BubbleSort bs = new BubbleSort();
        int[] numbers = { 16, 99, 6, 8, 12, 22, 86, 88, 66 };
        int[] result = bs.bubbleSort(numbers);
        for (int num = 0; num < result.length; num++) {
            System.out.print(result[num] + " ");
        }
        System.out.println();
    }
}

第四题

“你面前有两扇门,其中一扇门内藏着宝藏,但如果你不小心闯入另一扇门,只能痛苦地慢慢死掉……”

这一听就是那种经典的最令人头痛的一类问题,但其实与其他问题相比,这只是个热身。在这两扇门后面,有两个人,这两个人都知道哪扇门后有宝藏,哪扇门擅闯者死,而这两个人呢,一个人只说真话,一个人只说假话。

谁说真话谁说假话?那就要看你有没有智慧自己找出来了,游戏规则是,你只能问这两个人每人一个问题。

那么,你问什么问题?问哪个人?根据他们的回答,你又该怎么做?

答案: 问两个人:" 你知道另外一个人会说哪个门有宝藏?"


假设 第一个人说真话,第二个人说假话.

A门里面有宝藏,B门没有.

第一个人认为第二个人会说假话,所以说B门里面有宝藏.

第二个人知道第一个人说真话,所以他说了假话,说B门里面有宝藏.

所以第一个人和第二个人都会说B门有宝藏,只要相反就能得到宝藏.


第五题

二分法排序

在插入第i个元素时,对前面的0~i-1元素进行折半,先跟他们 
中间的那个元素比,如果小,则对前半再进行折半,否则对后半 
进行折半,直到left>right,然后再把第i个元素前1位与目标位置之间 
的所有元素后移,再把第i个元素放在目标位置上。
 

package interviewquestion;

public class BinarySort {

 private boolean isDebug = false;

 public int[] binarySort(int[] input) {   int length = input.length;   if (length <= 1) {    return input;   }

  for (int x = 1; x < length; x++) {    int[] temp = new int[x];    for (int num = 0; num < x; num++) {     temp[num] = input[num];    }    int[] temp2 = binaryResort(temp, input[x]);    for (int num = 0; num < temp2.length; num++) {     input[num] = temp2[num];    }   }   return input;  }

 public void printInts(int[] input) {   String str = "";   for (int x = 0; x < input.length; x++) {    str = str + input[x] + ",";   }   System.out.println(str);  }

 private int[] binaryResort(int[] olds, int inputNum) {   if (olds.length == 1) {    if (olds[0] > inputNum) {     int[] news = { inputNum, olds[0] };     if (isDebug) {      printInts(news);     }     return news;    } else {     int[] news = { olds[0], inputNum };     if (isDebug) {      printInts(news);     }     return news;    }   }   if (olds.length == 2) {    if (olds[0] > inputNum) {     int[] news = { inputNum, olds[0], olds[1] };     return news;    } else {     if (olds[1] < inputNum) {      int[] news = { olds[0], olds[1], inputNum };      return news;     }    }    int[] news = { olds[0], inputNum, olds[1] };    if (isDebug) {     printInts(news);    }    return news;   }   int x = olds[olds.length / 2];   if (inputNum == x) {    int[] news = new int[olds.length + 1];    for (int num = 0; num < (olds.length / 2); num++) {     news[num] = olds[num];    }    news[olds.length / 2] = inputNum;    for (int num = (olds.length / 2 + 1); num < (olds.length + 1); num++) {     news[num] = olds[num - 1];    }    if (isDebug) {     printInts(news);    }    return news;   }   boolean isOdd = false;   if (olds.length % 2 != 0) {    isOdd = true;   }   if (inputNum < x) {    int[] temp = new int[olds.length / 2];

   for (int num = 0; num < temp.length; num++) {     temp[num] = olds[num];    }    int[] temp2 = binaryResort(temp, inputNum);    int[] news = new int[olds.length + 1];    for (int num = 0; num < (olds.length + 1); num++) {     if (num < temp2.length) {      news[num] = temp2[num];     } else {      news[num] = olds[num - 1];     }    }    if (isDebug) {     printInts(news);    }    return news;   }   int[] temp = null;   if (isOdd) {    temp = new int[olds.length / 2];   } else {    temp = new int[olds.length / 2 - 1];   }   int beginNum = 0;   if (isOdd) {    beginNum = olds.length / 2 + 1;    if (isDebug) {     System.out.println("isOdd" + beginNum + "    " + olds.length);    }   } else {    beginNum = olds.length / 2 + 1;    if (isDebug) {     System.out.println("isEvent" + beginNum + "    " + olds.length);    }   }   for (int num = 0; num < temp.length; num++) {    temp[num] = olds[olds.length - temp.length + num];   }   if (isDebug) {    System.out.println("----------------");    printInts(temp);   }   int[] temp2 = binaryResort(temp, inputNum);   if (isDebug) {    printInts(temp2);   }   int[] news = new int[olds.length + 1];   int recordBeginNum2 = 0;   for (int num = 0; num < (olds.length + 1); num++) {    if (num >= beginNum) {     news[num] = temp2[recordBeginNum2];     recordBeginNum2++;    } else {     news[num] = olds[num];    }   }   if (isDebug) {    printInts(news);   }   return news;  } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值