算法题

前言

总结一些算法题目,面试使用
源码地址: https://github.com/litttlefisher/algorithm

1. 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.littlefisher.algorithm;

/**
* 兔子问题
*
* 斐波那契数列求值
*
* 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子, 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
*
* 程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
*
* @author jinyn22648
* @version $$Id: Rabbit.java, v 0.1 2018/5/6 下午2:20 jinyn22648 Exp $$
*/
public class Rabbit {

/**
* for循环实现
*
* @param month 月份
*/
public void rabbit(Integer month) {
Long f1 = 1L;
Long f2 = 1L;
Long f;
for (Integer i = 3; i <= month; i++) {
f = f1 + f2;
f1 = f2;
f2 = f;
System.out.println("第" + i + "个月的兔子对数:" + f2);
}
}

/**
* 递归方法实现
*
* @param month 月份
*/
public int fib(int month) {
// 第一个月
final Integer monthOne = 1;
// 第二个月
final Integer monthTwo = 2;
if (month == monthOne || month == monthTwo) {
return 1;
} else {
return fib(month - 1) + fib(month - 2);
}
}

public static void main(String[] args) {
// 假设持续到第15个月
final Integer month = 15;
Rabbit rabbit = new Rabbit();
// 第一种实现方式
rabbit.rabbit(month);
System.out.println("----------------我是分割线-----------------");
// 第二种实现方式
for (int i = 1; i <= month; i++) {
System.out.println("第" + i + "个月的兔子对数:" + rabbit.fib(i));
}
}
}

 

2. 题目:打印出所有的”水仙花数”,所谓”水仙花数”是指一个三位数,其各位数字立方和等于该数本身。

例如:
153是一个”水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.littlefisher.algorithm;

/**
* 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
*
* 例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
*
* 程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
*
* @author jinyn22648
* @version $$Id: Daffodils.java, v 0.1 2018/5/6 下午3:41 jinyn22648 Exp $$
*/
public class Daffodils {

/** 最小三位数 */
private static final Integer THREE_DIGITS_MIN = 100;

/** 最大三位数 */
private static final Integer THREE_DIGITS_MAX = 999;

public void getDaffodils() {
// 个位数
int singleDigit;
// 十位数
int tensDigit;
// 百位数
int hundredsDigit;
// 各位数字立方和
int data;
for (int i = THREE_DIGITS_MIN; i <= THREE_DIGITS_MAX; i++) {
hundredsDigit = i / 100;
tensDigit = (i - 100 * hundredsDigit) / 10;
singleDigit = i - 100 * hundredsDigit - 10 * tensDigit;
data = singleDigit * singleDigit * singleDigit + tensDigit * tensDigit * tensDigit
+ hundredsDigit * hundredsDigit * hundredsDigit;
if (data == i) {
System.out.println(i);
}
}

}

public static void main(String[] args) {
new Daffodils().getDaffodils();
}
}

 

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

程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:

  1. 如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
  2. 如果n!=k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
  3. 如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    package com.littlefisher.algorithm;

    /**
    * 任意整数分解 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
    *
    * 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
    *
    * (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
    *
    * (2)如果n>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
    *
    * (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
    *
    * @author jinyn22648
    * @version $$Id: ResolveNum.java, v 0.1 2018/5/6 下午4:07 jinyn22648 Exp $$
    */
    public class ResolveNum {

    public void resolveNum(Integer num) {
    int k = 2;
    System.out.print(num + "=");
    while (num > k) {
    if (num % k == 0) {
    System.out.print(k + "*");
    num = num / k;
    } else {
    k++;
    }
    }
    System.out.println(k);
    }

    public static void main(String[] args) {
    new ResolveNum().resolveNum(130);
    }
    }

4. 题目:输入两个正整数mn,求其最大公约数和最小公倍数。

程序分析:利用辗除法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.littlefisher.algorithm;

/**
* 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
*
* 程序分析:利用辗除法。
*
* @author jinyn22648
* @version $$Id: CommonDivisor.java, v 0.1 2018/5/6 下午4:17 jinyn22648 Exp $$
*/
public class CommonDivisor {

/**
* 辗除法
*
* @param a 正整数
* @param b 正整数
*/
public void gcd(Integer a, Integer b) {
int first = a;
int second = b;
System.out.println("a=" + a + ";b=" + b);
int temp;

if (a < b) {
temp = a;
a = b;
b = temp;
}
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
System.out.println("最大公约数为" + a);
System.out.println("最小公倍数为" + first * second / a);
}

/**
* while循环用法
*
* @param a 正整数
* @param b 正整数
*/
public void commonDivisor(Integer a, Integer b) {
int k = a > b ? b : a;
while (k > 1) {
if (a % k == 0 && b % k == 0) {
break;
}
k--;
}
System.out.println("最大公约数为" + k);
System.out.println("最小公倍数为" + a * b / k);

}

public static void main(String[] args) {
CommonDivisor commonDivisor = new CommonDivisor();
// 碾除法
commonDivisor.gcd(60, 400);
// while循环
commonDivisor.commonDivisor(60, 400);
}
}

 

5. 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

程序分析:利用while语句,条件为输入的字符不为’\n’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.littlefisher.algorithm;

/**
* 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
*
* 程序分析:利用while语句,条件为输入的字符不为'\n'.
*
* @author jinyn22648
* @version $$Id: StrIdentify.java, v 0.1 2018/5/6 下午4:48 jinyn22648 Exp $$
*/
public class StrIdentify {

public void strIdentify(String str) {
int abcCount = 0;
int spaceCount = 0;
int numCount = 0;
int otherCount = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
numCount++;
} else if (Character.isSpaceChar(c)) {
spaceCount++;
} else if (Character.isLetter(c)) {
abcCount++;
} else {
otherCount++;
}
}
System.out.println("字母个数" + abcCount);
System.out.println("数字个数" + numCount);
System.out.println("空格个数" + spaceCount);
System.out.println("其他字符个数" + otherCount);
}

public static void main(String[] args) {
new StrIdentify().strIdentify("alk jf;oa9 uwr02 34 u02ifl sdjf;a o9u3 2 3 ");
}

}

 

6. 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

程序分析:关键是计算出每一项的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.littlefisher.algorithm;

/**
* 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)
*
* 程序分析:关键是计算出每一项的值
*
* @author jinyn22648
* @version $$Id: NumAdd.java, v 0.1 2018/5/6 下午4:56 jinyn22648 Exp $$
*/
public class NumAdd {

public void numAdd(Integer num, Integer count) {
int result = 0;
for (int i = count; i > 0; i--) {
result += i * num * Math.pow(10, (count - i));
}
System.out.println("result=" + result);
}

public static void main(String[] args) {
new NumAdd().numAdd(4, 4);
}
}

 

7. 题目:一个数如果恰好等于它的因子之和,这个数就称为”完数”。例如6=1+2+3.编程 找出1000以内的所有完数。

暂时没理解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package cn.edu.hit;

/**
* 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如6=1+2+3.编程 找出1000以内的所有完数。
*
* @author tonylp
*/
public class wanShu {
public static void main(String[] args) {
int k = 2;
int num = 0;
int temp = 1;
int j = 0;
for (num = 1; num <= 1000; num++) {
k = 2;
temp = 1;
j = num;
while (j >= k) {
if (j % k == 0) {
temp += k;
j = j / k;
} else {
k++;
}
}
if (temp == num) {
System.out.println(temp);
}

}
}
}

 

8. 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.littlefisher.algorithm;

/**
* 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
*
* @author jinyn22648
* @version $$Id: Ball.java, v 0.1 2018/5/6 下午8:48 jinyn22648 Exp $$
*/
public class Ball {

public void ball(Double high, Integer count) {
double currentHigh = high;
double sum = high;
for (int i = 2; i <= count; i++) {
currentHigh = currentHigh * 0.5;
sum += currentHigh * 2;
}
System.out.println("currentHigh=" + currentHigh);
System.out.println("sum=" + sum);
}

public static void main(String[] args) {
new Ball().ball(1000d, 2);
}
}

9. 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.littlefisher.algorithm;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
*
* @author jinyn22648
* @version $$Id: NoRepeatingNumber.java, v 0.1 2018/5/6 下午9:04 jinyn22648 Exp $$
*/
public class NoRepeatingNumber {

public void noRepeatingNumber(List<Integer> numbers) {
Set<Integer> numberList = new HashSet<>();
for (Integer a : numbers) {
for (Integer b : numbers) {
for (Integer c : numbers) {
String number = "" + a + b + c;

numberList.add(Integer.valueOf(number));
}
}
}
System.out.println(numberList);
}

public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(1);
numbers.add(1);
numbers.add(0);
new NoRepeatingNumber().noRepeatingNumber(numbers);
}
}

 

10. 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?

程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.edu.hit;
import java.util.Scanner;
/**
* 题目:企业发放的奖金根据利润提成。 利润(I)低于或等于10万元时,奖金可提10%; 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,
* 高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%; 40万到60万之间时高于40万元的部分,可提成3%;
* 60万到100万之间时,高于60万元的部分,可提成1.5%, 高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
* 1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
*
* @author tonylp
*/
public class moneyAward {
public static void main(String[] args) {
double rate1 = 0.1, rate2 = 0.075, rate3 = 0.05, rate4 = 0.03, rate5 = 0.014, rate6 = 0.01;
long reward = 0;
System.out.println("请输入利润:");
Scanner sc = new Scanner(System.in);
long money = sc.nextLong();
if (money >= 0 && money <= 100000) {
reward = (long) (money * rate1);
} else if (money <= 200000) {
reward = (long) (100000 * rate1 + (money - 100000) * rate2);
} else if (money <= 400000) {
reward = (long) (100000 * rate1 + 100000 * rate2 + (money - 200000) * rate3);
} else if (money <= 600000) {
reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3
+ (money - 400000) * rate4);
} else if (money <= 1000000) {
reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3 + 200000
* rate4 + (money - 6000000) * rate5);
} else {
reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3 + 200000
* rate4 + 400000 * rate5 + (money - 1000000) * rate6);
}
System.out.println("奖金为:"+reward);
}
}

 

11. 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.edu.hit;
/**
* 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
* 1.程序分析:在10万以内判断,先将该数加上100后再开方,
* 再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
* 请看具体分析:
* @author tonylp
*/
public class findNumber {
public static void main(String[] args) {
for(int i=0;i<100000;i++){
if((Math.sqrt(i+100)%1==0)&&(Math.sqrt(i+168)%1 == 0)){
System.out.println(i);
}
}
}
}

 

12. 题目:输入某年某月某日,判断这一天是这一年的第几天?

程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.littlefisher.algorithm;

import java.util.Calendar;
import java.util.Date;

/**
* 题目:输入某年某月某日,判断这一天是这一年的第几天?
*
* @author jinyn22648
* @version $$Id: DaysOfYear.java, v 0.1 2018/5/7 下午7:48 jinyn22648 Exp $$
*/
public class DaysOfYear {

public void daysOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int days = 0;
int[] arr = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 是否闰年
boolean isLeapYear = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
if (isLeapYear) {
arr[1] = 29;
}
for (int i = 0; i < month - 1; i++) {
days += arr[i];
}
days += day;
System.out.println("天数为:" + days);
}

public static void main(String[] args) {
new DaysOfYear().daysOfYear(new Date());
}

}

 

13. 题目:输入三个整数x,y,z,请把这三个数由小到大输出

程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.littlefisher.algorithm;

import java.util.ArrayList;
import java.util.List;

/**
* 题目:输入三个整数x,y,z,请把这三个数由小到大输出。
*
* @author jinyn22648
* @version $$Id: Compare.java, v 0.1 2018/5/7 下午7:56 jinyn22648 Exp $$
*/
public class Compare {

public void compare(List<Integer> numberList) {
int minNum;
for (int i = 0; i < numberList.size(); i++) {
minNum = numberList.get(i);
int currentIndex = i;
for (int j = i + 1; j < numberList.size(); j++) {
if (numberList.get(j) < minNum) {
currentIndex = j;
}
}
swap(numberList, i, currentIndex);
}
System.out.println(numberList);
}

private void swap(List<Integer> numberList, int i, int j) {
int temp = numberList.get(i);
numberList.set(i, numberList.get(j));
numberList.set(j, temp);
}

public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
for (int item : arr) {
list.add(item);
}
new Compare().compare(list);
}

}

 

14. 题目:输出9*9口诀。

程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.littlefisher.algorithm;

/**
* 题目:输出9*9口诀。
*
* @author jinyn22648
* @version $$Id: Nine.java, v 0.1 2018/5/7 下午8:07 jinyn22648 Exp $$
*/
public class Nine {

public void nine() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
System.out.println(j + "*" + i + "=" + j * i);
}
}
}

public static void main(String[] args) {
new Nine().nine();
}
}

 

15. 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

程序分析:采取逆向思维的方法,从后往前推断。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.littlefisher.algorithm;

/**
* 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
*
* @author jinyn22648
* @version $$Id: MonkeyEatPeach.java, v 0.1 2018/5/7 下午8:09 jinyn22648 Exp $$
*/
public class MonkeyEatPeach {

public void monkeyEatPeach(Integer days) {
int sum = 1;
for (int i = 1; i <= days; i++) {
sum = (sum + 1) * 2;
}
System.out.println("总数为:" + sum);
}

public static void main(String[] args) {
new MonkeyEatPeach().monkeyEatPeach(10);
}

}

 

16. 题目:打印出如下图案(菱形)

1
2
3
4
5
6
7
*
***
******
********
******
***
*

程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cn.edu.hit;

/**
* 题目:打印出如下图案(菱形) * *** ****** ******** ****** *** *
* 1.程序分析:先把图形分成两部分来看待,前四行一个规律, 后三行一个规律,利用双重 for循环, 第一层控制行,第二层控制列。
*
* @author tonylp
*/
public class lingXing {
public static void main(String[] args) {
int[] arr = { 1, 3, 6, 8, 6, 3, 1 };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < arr[i]; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int i = 4; i < arr.length; i++) {
for (int j = arr[i]; j < 8; j++) {
System.out.print(" ");
}
for (int j = 0; j < arr[i]; j++) {
System.out.print("*");
}
System.out.println("");
}
}

}

 

17. 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。

程序分析:请抓住分子与分母的变化规律。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package cn.edu.hit;

package com.littlefisher.algorithm;

/**
* 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
*
* @author jinyn22648
* @version $$Id: FractionAdd.java, v 0.1 2018/5/7 下午8:15 jinyn22648 Exp $$
*/
public class FractionAdd {

public void fractionAdd(Integer count) {
int[] numberArr = new int[count + 1];
numberArr[0] = 1;
numberArr[1] = 2;
numberArr[2] = 3;

long sumNumerator = numberArr[1] * numberArr[1] + numberArr[2] * numberArr[0];
long sumDenominator = numberArr[0] * numberArr[1];
for (int i = 3; i <= count; i++) {
numberArr[i] = numberArr[i - 1] + numberArr[i - 2];
int currentNumerator = numberArr[i];
int currentDenominator = numberArr[i - 1];
System.out.println("当前分子为: " + currentNumerator + " 当前分母为: " + currentDenominator);
sumNumerator = sumNumerator * currentDenominator + currentNumerator + sumDenominator;
sumDenominator = sumDenominator * currentDenominator;
System.out.println("总和的分子为: " + sumNumerator + " 当前总和分母为: " + sumDenominator);
System.out.println("当前总和为: " + (sumNumerator / sumDenominator));
}

}

public static void main(String[] args) {
new FractionAdd().fractionAdd(10);
}
}

 

18. 题目:求1+2!+3!+…+20!的和

程序分析:此程序只是把累加变成了累乘。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.littlefisher.algorithm;

/**
* 题目:求1+2!+3!+...+20!的和
*
* @author jinyn22648
* @version $$Id: FactorialSum.java, v 0.1 2018/5/7 下午8:35 jinyn22648 Exp $$
*/
public class FactorialSum {

public void factorialSum(int count) {
int sum = 0;
for (int i = 1; i <= count; i++) {
int result = factorial(i);
System.out.println("当前阶乘结果: " + result);
sum += result;
}
System.out.println("总和为: " + sum);
}

private int factorial(int number) {
int result = 1;
while (number != 0) {
result *= number;
number--;
}
return result;
}

public static void main(String[] args) {
new FactorialSum().factorialSum(10);
}
}

 

19. 题目:利用递归方法求5!

程序分析:递归公式:fn=fn_1*4!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.littlefisher.algorithm;

/**
* 利用递归方法求5!
*
* @author jinyn22648
* @version $$Id: Factorial.java, v 0.1 2018/5/7 下午8:41 jinyn22648 Exp $$
*/
public class Factorial {

private int factorial(int number) {
if (number > 0) {
return factorial(number - 1) * number;
} else {
return 1;
}
}

public static void main(String[] args) {
int sum = new Factorial().factorial(5);
System.out.println("阶乘为: " + sum);
}
}

 

20. 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.littlefisher.algorithm;

/**
* 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
*
* @author jinyn22648
* @version $$Id: Age.java, v 0.1 2018/5/7 下午8:48 jinyn22648 Exp $$
*/
public class Age {

private int age(Integer count) {
if (count > 1) {
return age(count - 1) + 2;
} else {
return 10;
}
}

public static void main(String[] args) {
int age = new Age().age(5);
System.out.println("年龄为: " + age);
}
}

 

21. 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.littlefisher.algorithm;

/**
* 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
*
* @author jinyn22648
* @version $$Id: Number.java, v 0.1 2018/5/7 下午8:53 jinyn22648 Exp $$
*/
public class Number {

public void number(Integer number) {
String str = String.valueOf(number);
System.out.println(str + "是" + str.length() + "位数");
for (int i = str.length(); i > 0; i--) {
System.out.print(i);
}
}

public static void main(String[] args) {
new Number().number(12345);
}

}

22. 题目:求100之内的素数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.littlefisher.algorithm;

/**
* 题目:求100之内的素数
*
* @author jinyn22648
* @version $$Id: PrimeNumber.java, v 0.1 2018/5/7 下午8:57 jinyn22648 Exp $$
*/
public class PrimeNumber {

public void primeNumber(Integer max) {
for (int i = 1; i <= max; i++) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {
break;
}
if (j == i - 1) {
System.out.println(i + "是素数");
}
}
}
}

public static void main(String[] args) {
new PrimeNumber().primeNumber(100);
}
}

23. 题目:打印出杨辉三角形(要求打印出10行如下图)

1
2
3
4
5
6
     1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.littlefisher.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
* 题目:打印出杨辉三角形
*
* @author jinyn22648
* @version $$Id: YangHuiTriangle.java, v 0.1 2018/5/12 上午9:45 jinyn22648 Exp $$
*/
public class YangHuiTriangle {

public void yangHuiTriangle(Integer line) {
final Integer firstNumber = 1;
Map<Integer, Integer[]> lineMap = new HashMap<>(line);
Integer[] numberArr;
for (int i = 1; i <= line; i++) {
numberArr = new Integer[i];
lineMap.put(i, numberArr);
numberArr[0] = firstNumber;
numberArr[i - 1] = firstNumber;
if (numberArr.length == 1) {
continue;
}
Integer[] lastNumberArr = lineMap.get(i - 1);
for (int j = 1; j < lastNumberArr.length; j++) {
Integer sum = lastNumberArr[j - 1] + lastNumberArr[j];
numberArr[j] = sum;
}
}
for (Integer lineNum : lineMap.keySet()) {
if (lineMap.get(lineNum) != null) {
for (Integer num : lineMap.get(lineNum)) {
System.out.print(num + " ");
}
}
System.out.println();
}
}

public static void main(String[] args) {
new YangHuiTriangle().yangHuiTriangle(10);
}
}

24. 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.littlefisher.algorithm;

/**
* 题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
*
* @author jinyn22648
* @version $$Id: Quit.java, v 0.1 2018/5/7 下午9:01 jinyn22648 Exp $$
*/
public class Quit {

private static final Integer OUT = 1;

private static final Integer IN = 0;

public void quit(Integer num) {
int[] arr = new int[num];
int i = 0;
int t = 0;
while (num > 1) {
if (arr[i] == 0) {
t++;
if (t == 3) {
t = 0;
arr[i] = OUT;
num--;
}
}
i++;
if (i == arr.length) {
i = 0;
}
}

for (int j = 0; j < arr.length; j++) {
if (arr[j] == IN) {
System.out.println(j + 1);
}
}
}

public static void main(String[] args) {
new Quit().quit(100);
}

}

25. 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.littlefisher.algorithm;

/**
* 题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
*
* @author jinyn22648
* @version $$Id: MonkeyEatPeach.java, v 0.1 2018/5/7 下午8:09 jinyn22648 Exp $$
*/
public class MonkeyEatPeach2 {

public void monkeyEatPeach(Integer minLastPeach, Integer numOfMonkey) {
int sum = minLastPeach;
for (int i = 1; i <= numOfMonkey; i++) {
sum = sum * 5 + 1;
}
System.out.println("总数为:" + sum);
}

public static void main(String[] args) {
new MonkeyEatPeach2().monkeyEatPeach(1, 5);
}

}

26. 题目:一个偶数总能表示为两个素数之和。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.littlefisher.algorithm;

/**
* 题目:一个偶数总能表示为两个素数之和。
*
* @author jinyn22648
* @version $$Id: EventNumber.java, v 0.1 2018/5/12 下午3:10 jinyn22648 Exp $$
*/
public class EventNumber {

public void eventNumber(Integer number) {
if (number <= 0 || number % 2 != 0) {
System.out.println("不是非零偶数");
}

for (int i = 1; i <= number / 2; i++) {
if (checkIsPrimeNumber(i) && checkIsPrimeNumber(number - i)) {
System.out.println(number + "是" + i + "和" + (number - i) + "这2个素数之和");
}
}
}

private boolean checkIsPrimeNumber(Integer number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= number - 1; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}

public static void main(String[] args) {
new EventNumber().eventNumber(200);
}
}

27. 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.littlefisher.algorithm;

import java.util.Arrays;

/**
* 题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
*
* @author jinyn22648
* @version $$Id: ExchangeNum.java, v 0.1 2018/5/12 下午3:22 jinyn22648 Exp $$
*/
public class ExchangeNum {

public void exchangeNum(int[] arr) {
int max = 0;
int min = arr.length - 1;
for (int i = 1; i < arr.length; i++) {
if (arr[0] < arr[i]) {
max = i;
}
}
swap(arr, 0, max);
for (int i = 0; i < arr.length - 1; i++) {
if (arr[arr.length - 1] > arr[i]) {
min = i;
}
}
swap(arr, arr.length - 1, min);
Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
}

private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
new ExchangeNum().exchangeNum(arr);
}
}

28. 题目:取一个整数a从右端开始的4~7位。

程序分析:可以这样考虑:

  • 先使a右移4位。
  • 设置一个低4位全为1,其余全为0的数。可用~(~0 << 4)
  • 将上面二者进行&运算。
1
2
3
4
5
6
7
8
9
public class Ex32 {
public static void main(String[] args)
{
int a=0;
long b=18745678;
a=(int) Math.floor(b % Math.pow(10,7)/Math.pow(10, 3));
System.out.println(a);
}
}

29. 题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Random;
public class ArraySort {
public static void main(String[] args)
{ int temp=0;
int myarr[] = new int[12];
Random r=new Random();
for(int i=1;i<=10;i++)
myarr[i]=r.nextInt(1000);
for (int k=1;k<=10;k++)
System.out.print(myarr[k]+",");
for(int i=1;i<=9;i++)
for(int k=i+1;k<=10;k++)
if(myarr[i]>myarr[k])
{
temp=myarr[i];
myarr[i]=myarr[k];
myarr[k]=temp;
}
System.out.println("");
for (int k=1;k<=10;k++)
System.out.print(myarr[k]+",");

myarr[11]=r.nextInt(1000);
for(int k=1;k<=10;k++)
if(myarr[k]>myarr[11])
{
temp=myarr[11];
for(int j=11;j>=k+1;j--)
myarr[j]=myarr[j-1];
myarr[k]=temp;
}
System.out.println("");
for (int k=1;k<=11;k++)
System.out.print(myarr[k]+",");
}
}

 

30. 题目:求一个3*3矩阵对角线元素之和

程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。

1
2
3
4
5
6
7
8
9
10
11
12
public class Ex29 {
public static void main(String[] args){
double sum=0;
int array[][]={{1,2,3},{4,5, 6},{7,7,8}};
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
if(i==j)
sum=sum + array[i][j];
}
System.out.println( sum);
}
}

 

31. 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.littlefisher.algorithm;

/**
* 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
*
* @author jinyn22648
* @version $$Id: PalindromeNumber.java, v 0.1 2018/5/12 下午3:39 jinyn22648 Exp $$
*/
public class PalindromeNumber {

public void palindromeNumber(Integer number) {
char[] chars = number.toString().toCharArray();
int i = 0;
for (; i <= chars.length / 2; i++) {
if (chars[i] != chars[chars.length - i - 1]) {
break;
}
}
if (i == chars.length / 2 + 1) {
System.out.println(number + "是回文数");
} else {
System.out.println(number + "不是回文数");
}
}

public static void main(String[] args) {
new PalindromeNumber().palindromeNumber(12344321);
}

}

32. 题目:给你一个数组{0,1,2,3,….n},其中有一个数字缺失,请把缺失的数字找出来

思路:

  • 创建一个数组(题目数组的长度+1,因为题目的数组缺失了一个)
  • 创建的数组元素用特殊的符号(数字)来进行填满
  • 将题目给出的数组遍历并填充到创建的数组上,用index(0,1,2,3..)替代
  • 最后遍历创建的数组,哪个还是特殊的符号就是缺失的数字,返回index(缺失的数字)即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.littlefisher.algorithm;

/**
* 题目:给你一个数组{0,1,2,3,....n},其中有一个数字缺失,请把缺失的数字找出来
*
* @author jinyn22648
* @version $$Id: MissingNumber.java, v 0.1 2018/5/12 下午3:48 jinyn22648 Exp $$
*/
public class MissingNumber {

public void missingNumber(int[] arr) {
// 套用等差求和公式
int sum = (arr[0] + arr[arr.length - 1]) * (arr.length + 1) / 2;
// 遍历数组,得出的sum减去数组每一位元素,最后即是缺失的数字
for (int value : arr) {
sum = sum - value;
}
System.out.println("缺失的数字是:" + sum);
}

public static void main(String[] args) {
int[] arr = {0, 1, 2, 3, 4, 5, 6, 8, 9, 10};
new MissingNumber().missingNumber(arr);
}
}

配置Gitlab CI
posted @ 2019-07-31 16:00  qxwang  阅读(39)  评论(0)    收藏  举报