面試題(二)

題目:古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數爲多少?

public class RabbitQues {

    public static int numberOfRabbits(int month){
        int[] total = new int[month];
        if( month < 3){
            return 1;
        }
        total[0] = 1;
        total[1] = 1;
        for(int x =2 ; x< month; x++){
            total[x] = total[x-1] + total[x-2];
        }
        return total[month - 1];
    }
    }

}

打印出所有的 "水仙花數 ",所謂 "水仙花數 "是指一個三位數,其各位數字立方和等於該數本身。例如:153是一個 "水仙花數 ",因爲153=1的三次方+5的三次方+3的三次方。

public class NarcissusQues {
    public static ArrayList<Integer> getNarcissus(){
        ArrayList<Integer> narcissuses = new ArrayList<Integer>();
        for(int x = 100; x < 1000;x ++){
            int  hundreds = x/100;
            int tens = x/10 - hundreds*10;
            int units = x%10;

            if(x == (hundreds*hundreds*hundreds + tens*tens*tens + units*units*units)){
                narcissuses.add(x);
            }
        }
        return narcissuses;
    }

}

題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最後問第一個人,他說是10歲。請問第五個人多大?

public class AgeQues {
   public int calculateAge(int sAge, int addAge, int pNum, int totalP){
       if(pNum == totalP){
           return sAge;
       }
        return calculateAge(sAge + addAge, addAge, ++pNum, totalP);
   }
}

題目:海灘上有一堆桃子,五隻猴子來分。第一隻猴子把這堆桃子憑據分爲五份,多了一個,這隻猴子把多的一個扔入海中, 拿走了一份。第二隻猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中,拿走了一份,第三、第四、第五隻猴子都是這樣做的,問海灘上原來最少有多少個桃子?

public class MonkeyPeachQues {
    public int calculateSmallestPeaches(){
        int num = 1;
        int numPeach = 0;
       while(true){
           numPeach = num * 5 + 1;
           for(int numMonkey =1; numMonkey<=5;numMonkey++){
               if(numPeach <= 5){
                  break;
               }
               if(numPeach%5!=1){
                   break;
               }
               if(numMonkey==5){
                   return num*5+1;
               }
               numPeach = (numPeach-1)*4/5;
           }
           num++;
       }
    }
    public static void main(String args[]){
        System.out.println("The the smallest number of peaches : " + new MonkeyPeachQues().calculateSmallestPeaches()); //3121 peaches.
    }
}

題目:一個偶數總能表示爲兩個素數之和。

public class PrimeNumQues {
    public static boolean isPrime(int num){
        if(num<2){
            return false;
        }
        if(num==2){
            return true;
        }
        for(int i=2;i<Math.sqrt(num);i++){
           if(num%i==0){
               return false;
           }
        }
        return true;
    }

    public int[] analysisEven(int even) throws Exception{
        if(even%2!=0){
            throw new Exception("Not an even number.");
        }
        if(even <= 2){
            throw new Exception("Even number should be bigger than 2.");
        }
        int[] results = new int[]{-1,-1};
        int half = even/2;
        for(int num = 2; num <= half; num++){
            if(isPrime(num)){
                if(isPrime(even - num)){
                    results[0] = num;
                    results[1] = even - num;
                    System.out.println("Result: " + results[0] + ", " + results[1]);
                    return results;
                }
            }
        }
        throw new Exception("This even number can not be divided into two prime numbers.");
    }
}

題目:一球從100米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在   第10次落地時,共經過多少米?第10次反彈多高?

public class BallDownQues {
   public static double calculateTotalHeight(double beginHeight, int targetTimes){
       double downHeight = 0;
       double reboundHeight = 0;
       double tempHeight = beginHeight;
       for(int num=0;num<targetTimes;num++){
           downHeight = downHeight + tempHeight;
           tempHeight = tempHeight/2;
           if(num < (targetTimes-1)){
              reboundHeight = reboundHeight + tempHeight;
              System.out.println("Times: " + num + "  Down: " + downHeight + "  Rebound: " + reboundHeight);
           }else{
              System.out.println("Times: " + num + " Down: " + downHeight);
              System.out.println("Times: " + num + " This time's rebound height: " + tempHeight);
           }
       }
       double totalHeight = reboundHeight+downHeight;
       System.out.println("Total Height: " + totalHeight);
       return totalHeight;
   }
    public static void main(String args[]){
        BallDownQues.calculateTotalHeight(100,10)  ;
    }
}

題目: 用JAVA檢查字符串爲數字

public static boolean isDigit(String input){
        char[] chars = input.toCharArray();
        int charLen = chars.length;
        if(charLen==0){
            return false;
        }
        for(int num =0;num<charLen;num++){
              if(!Character.isDigit(chars[num])){
                  return false;
              }
        }
        return true;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章