算法面试题java

时间:2022-03-21 09:43:11

1、兔子问题:已知一对兔子从第三个月开始,每一个月可以生一对小兔子,而一对兔子出生后.第三个月开始生小兔子假如一年内没有发生死亡,则一对兔子一年内能繁殖成多少对?

    分析:先看下6个月的兔子对数:1 、1、2、3、5、8...,可以看出,从第三个月开始,每个月的兔子对数为前一个月的兔子对数加上前两个月的兔子对数。

    f(n) = f(n-1) + f(n-2)

    实现代码:

       

 1 public class Rabbit {
2
3
4 public int getRabbit(int month){
5
6 int a = 1;//前两个月的兔子对数
7 int b = 1;//前一个月的兔子对数
8 int c = 1;//当月的兔子对数
9
10 for(int i = 1;i <= month ; i++){
11 if(i == 1||i == 2){
12 c = 1;
13 }else{
14 //为计算下一月的值做准备
15 a = b;//把前一个月的值,赋予前两个月
16 b = c;//把前当月的值赋予前一个月
17
18 c = b + a;//
19 }
20 }
21
22 return c;
23
24 }
25
26 //递归
27 public int getRabbit2(int month){
28
29 if(month == 1||month ==2){
30 return 1;
31 }else{
32 return getRabbit2(month -1) + getRabbit2(month -2);
33 }
34
35 }
36
37
38 public static void main(String[] args) {
39 Rabbit r = new Rabbit();
40 System.out.println(r.getRabbit(16));
41 System.out.println(r.getRabbit2(16));
42 }
43
44 }

 

 

    2、求n的阶乘(n!)

         

 1 public class Factorial {
2
3
4 public static Long getFactorial(Long n){
5
6 if(n <=0 ){
7 return 0L;
8 }else if(n == 1){
9 return 1L;
10 }else{
11 return getFactorial(n-1)*n;
12 }
13
14 }
15
16
17 public static void main(String[] args) {
18 System.out.println(Factorial.getFactorial(3L));
19 }
20
21 }

 

3、求一定范围内的所有质数(素数)

     判断质数(素数)的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 
则表明此数不是素数,反之是素数。

public class Zhishu {

public static boolean isZhishu(int n){
//此方法返回一个正平方根。如果参数是NaN或小于为零,那么结果为NaN Double n_sqrt
= Math.sqrt(n);
for(int i = 2;i<=n_sqrt;i++){
if(n % i == 0){
return false;
}
}
return true;
}

public static void main(String[] args) {
for(int i = 2;i < 100;i++){
if(Zhishu.isZhishu(i)){
System.out.println(i);
}
}
}

}

 

    4、水仙花数

        所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。

        比如:153 = 1*1*1 + 5*5*5+3*3*3 = 1+125+27 

        

/**
* 水仙花是一个三位数,各位上的数字的立方和等于此值的本省
* */
public class Shuixianhua {

public static boolean isSXH(int num){
boolean flag = false;
int geWei = 0;
int shiWei = 0;
int baiWei = 0;

//获取百位、十位、个位的数字
baiWei = num / 100;
shiWei = num % 100 / 10;//(num - baiWei * 100)/10
geWei = num % 100 % 10 ;//(num - baiWei * 100 - shiWei *10 )

//if(baiWei * baiWei * baiWei + shiWei * shiWei* shiWei + geWei * geWei *geWei == num){
//pow 函数的使用 (math.sqrt())
if(Math.pow(baiWei, 3)+Math.pow(shiWei, 3)+Math.pow(geWei, 3) == num){
flag = true;
}
return flag;
}

public static void main(String[] args) {
for(int i = 100;i< 900;i++){
if(isSXH(i)){
System.out.println(i);
}
}

}

}

  

    5、完全数

        如果一个数恰好等于它的因子之和,则称该数为“完全数”

        

/**
* 它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。
* 如果一个数恰好等于它的因子之和,则称该数为“完全数”
*
*/
public class PerfectNumber {

public static void main(String[] args) {

for (int i = 2; i < 1000; i++) {
int sum = 0;
// 查找因数
for (int j = 1; j < i; j++) {
if (i % j == 0) {
sum
+= j;
}
}
if (sum == i)
System.out.println(i);
}
}
}

 

    6、乘法表

     

public class ChengF {

public static void main(String[] args) {
for(int i = 1 ;i <= 9;i++){
for(int j = 1; j <= i;j++){
System.out.print(j
+ "*"+ i +"="+(i*j)+" ");
if(i == j) System.out.println();
}
}
}

}

 

    7、三角形

 

public class Triangle {

public static void main(String[] args) {
int i = 0;//控制行
int j = 0;//控制每行的个数
//分两部分打印,一部分是增加*的个数;一部分是减少*的个数。 for(i = 1;i <= 4;i++){
for(j = 1;j <= i*2 -1;j++ ){
System.out.print(
"*");
}
System.out.println();
}

for(i = 3;i >= 1;i--){
for(j = 1;j <= i*2 -1;j++ ){
System.out.print(
"*");
}
System.out.println();
}

}


结果:

*
***
*****
*******
*****
***
*

 

 

8、猴子摘了一些桃子,第一天吃了一半,又多吃了一个, 第二天又吃了一半,又多吃一个, 以后每天都是,第10天剩下一个,请问猴子一共摘了多少桃子。

    

/**
* 猴子摘了一些桃子,第一天吃了一半,又多吃了一个,
* 第二天又吃了一半,又多吃一个,
* 以后每天都是,第10天剩下一个,请问猴子一共摘了多少桃子。
*
*/
public static void main(String[] args) {
System.out.println(Monkey.getTaoNum(
10));
}


public static int getTaoNum(int dayNum){
int taoNum = 1;
for(int i = dayNum;i >=1 ;i-- ){
taoNum
= (taoNum + 1)*2;
}
return taoNum;
}

 

9、将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5

 

public static void main(String[] args) {
Fenjie.fen(
90);
}

public static void fen(int num){
int a = 2;
while(num >= a){
if(num % a == 0){
System.out.print(a
+ "*");
num
= num / a;
}
else{
a
++;
}
}
}

 

10、输入两个正整数m和n,求其最大公约数和最小公倍数

/**
* 在循环中,只要除数不等于0,用较大数与较小的数取余,将小的一个数作
* 为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环
* 直到较小的数的值为0,返回 较大的数,此数即为最小公约数,最小公倍数为两数之积除以最小公倍数
*
*/
public static int getGongyue(int a,int b){
int tep;
if(a > b){
tep
= a;
a
= b;
b
= tep;
}

while(a !=0 ){
if(a == b){
return 1;
}
else{
int k = b % a;
b
= a;
a
= k;
}
}

return b;

}

public static void main(String[] args) {

System.out.println(GongyueGongbei.getGongyue(
16, 12));
System.out.println(
16 * 12 / GongyueGongbei.getGongyue(16, 12));

}

 

11、一球从100米高度*落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?

 static double height = 100;
static double distance = 0;

public static void main(String[] args) {
for(int i=1; i<=10; i++) {
double d = 0;//反弹到下次落地的距离
if(i == 1 ){
d
= height;
}
else{
d
= height*2;
}

distance
= distance + d ;
height
= height / 2;
}

System.out.println(
"路程:" + distance);
System.out.println(
"高度:" + height);
}