面试题(二)

题目:古典问题:有一对兔子,从出生后第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;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值