Java基础23-递归练习

  1. public class Test1 {
  2. Test2
    1. Test4
    2. Test5
    3. Test6
    4. Test7
    5. Test8

public class Test1 {

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
66
67
68
69
70
71
72
73
74
75
76
77
78
public class Test1 {

/**
* @param args
* 需求:1,从键盘接收一个文件夹路径,统计该文件夹大小
*
* 从键盘接收一个文件夹路径
* 1,创建键盘录入对象
* 2,定义一个无限循环
* 3,将键盘录入的结果存储并封装成File对象
* 4,对File对象判断
* 5,将文件夹路径对象返回

* 统计该文件夹大小
* 1,定义一个求和变量
* 2,获取该文件夹下所有的文件和文件夹listFiles();
* 3,遍历数组
* 4,判断是文件就计算大小并累加
* 5,判断是文件夹,递归调用
*/
public static void main(String[] args) {
//File dir = new File("F:\\day06");
//System.out.println(dir.length()); //直接获取文件夹的结果是0
File dir = getDir();
System.out.println(getFileLength(dir));
}

/*
* 从键盘接收一个文件夹路径
* 1,返回值类型File
* 2,参数列表无
*/
public static File getDir() {
//1,创建键盘录入对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件夹路径:");
//2,定义一个无限循环
while(true) {
//3,将键盘录入的结果存储并封装成File对象
String line = sc.nextLine();
File dir = new File(line);
//4,对File对象判断
if(!dir.exists()) {
System.out.println("您录入的文件夹路径不存在,请输入一个文件夹路径:");
}else if(dir.isFile()) {
System.out.println("您录入的是文件路径,请输入一个文件夹路径:");
}else {
//5,将文件夹路径对象返回
return dir;
}
}

}

/*
* 统计该文件夹大小
* 1,返回值类型long
* 2,参数列表File dir
*/
public static long getFileLength(File dir) { //dir = F:\day06\day07
//1,定义一个求和变量
long len = 0;
//2,获取该文件夹下所有的文件和文件夹listFiles();
File[] subFiles = dir.listFiles(); //day07 Demo1_Student.class Demo1_Student.java
//3,遍历数组
for (File subFile : subFiles) {
//4,判断是文件就计算大小并累加
if(subFile.isFile()) {
len = len + subFile.length();
//5,判断是文件夹,递归调用
}else {
len = len + getFileLength(subFile);
}
}
return len;
}
}

Test2

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
public class Test2 {

/**
* 需求:2,从键盘接收一个文件夹路径,删除该文件夹
*
* 删除该文件夹
* 分析:
* 1,获取该文件夹下的所有的文件和文件夹
* 2,遍历数组
* 3,判断是文件直接删除
* 4,如果是文件夹,递归调用
* 5,循环结束后,把空文件夹删掉
*/
public static void main(String[] args) {
File dir = Test1.getDir(); //获取文件夹路径
deleteFile(dir);
}

/*
* 删除该文件夹
* 1,返回值类型 void
* 2,参数列表File dir
*/
public static void deleteFile(File dir) {
//1,获取该文件夹下的所有的文件和文件夹
File[] subFiles = dir.listFiles();
//2,遍历数组
for (File subFile : subFiles) {
//3,判断是文件直接删除
if(subFile.isFile()) {
subFile.delete();
//4,如果是文件夹,递归调用
}else {
deleteFile(subFile);
}
}
//5,循环结束后,把空文件夹删掉
dir.delete();
}
}

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
public class Test3 {

/**
* 需求:3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
*
* 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
* 分析:
* 1,在目标文件夹中创建原文件夹
* 2,获取原文件夹中所有的文件和文件夹,存储在File数组中
* 3,遍历数组
* 4,如果是文件就用io流读写
* 5,如果是文件夹就递归调用
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File src = Test1.getDir();
File dest = Test1.getDir();
if(src.equals(dest)) {
System.out.println("目标文件夹是源文件夹的子文件夹");
}else {
copy(src,dest);
}
}
/*
* 把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
* 1,返回值类型void
* 2,参数列表File src,File dest
*/
public static void copy(File src, File dest) throws IOException {
//1,在目标文件夹中创建原文件夹
File newDir = new File(dest, src.getName());
newDir.mkdir();
//2,获取原文件夹中所有的文件和文件夹,存储在File数组中
File[] subFiles = src.listFiles();
//3,遍历数组
for (File subFile : subFiles) {
//4,如果是文件就用io流读写
if(subFile.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName())));

int b;
while((b = bis.read()) != -1) {
bos.write(b);
}

bis.close();
bos.close();
//5,如果是文件夹就递归调用
}else {
copy(subFile,newDir);
}
}
}
}

Test4

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
public class Test4 {

/**
* 需求:4,从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印, 例如:
* 把文件夹中的所有文件以及文件夹的名字按层级打印
* 分析:
* 1,获取所有文件和文件夹,返回的File数组
* 2,遍历数组
* 3,无论是文件还是文件夹,都需要直接打印
* 4,如果是文件夹,递归调用
* day07
* day08
* xxx.jpg
* yyy.txt
* Demo1_Consturctor.class
* Demo1_Consturctor.java
* Demo1_Student.class
* Demo1_Student.java
*/
public static void main(String[] args) {
File dir = Test1.getDir(); //获取文件夹路径
printLev(dir,0);
}

public static void printLev(File dir,int lev) {
//1,把文件夹中的所有文件以及文件夹的名字按层级打印
File[] subFiles = dir.listFiles();
//2,遍历数组
for (File subFile : subFiles) {
for(int i = 0; i <= lev; i++) {
System.out.print("\t");
}
//3,无论是文件还是文件夹,都需要直接打印
System.out.println(subFile);
//4,如果是文件夹,递归调用
if(subFile.isDirectory()) {
//printLev(subFile,lev + 1);
printLev(subFile,++lev);
}
}
}

}

Test5

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
public class Test5 {

/**
* * 不死神兔
* 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。
* 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡,
* 问:一对刚出生的兔子,一年内繁殖成多少对兔子?
* 1 1 2 3 5 8 13 21
* 1 = fun(1)
* 1 = fun(2)
* 2 = fun(1) + fun(2)
* 3 = fun(2) + fun(3)
*/
public static void main(String[] args) {
//demo1();
System.out.println(fun(8));
}

public static void demo1() {
//用数组做不死神兔
int[] arr = new int[8];
//数组中第一个元素和第二个元素都为1
arr[0] = 1;
arr[1] = 1;
//遍历数组对其他元素赋值
for(int i = 2; i < arr.length; i++) {
arr[i] = arr[i - 2] + arr[i - 1];
}
//如何获取最后一个数
System.out.println(arr[arr.length - 1]);
}

/*
* 用递归求斐波那契数列
*/
public static int fun(int num) {
if(num == 1 || num == 2) {
return 1;
}else {
return fun(num - 2) + fun(num - 1);
}
}
}

Test6

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
public class Test6 {

/**
* @param args
* 需求:求出1000的阶乘所有零和尾部零的个数,不用递归做
*/
public static void main(String[] args) {
/*int result = 1;
for(int i = 1; i <= 1000; i++) {
result = result * i;
}

System.out.println(result); //因为1000的阶乘远远超出了int的取值范围
*/
//demo1();
demo2();
}

public static void demo2() { //获取1000的阶乘尾部有多少个零
BigInteger bi1 = new BigInteger("1");
for(int i = 1; i <= 1000; i++) {
BigInteger bi2 = new BigInteger(i+"");
bi1 = bi1.multiply(bi2); //将bi1与bi2相乘的结果赋值给bi1
}
String str = bi1.toString(); //获取字符串表现形式
StringBuilder sb = new StringBuilder(str);
str = sb.reverse().toString(); //链式编程

int count = 0; //定义计数器
for(int i = 0; i < str.length(); i++) {
if('0' != str.charAt(i)) {
break;
}else {
count++;
}
}

System.out.println(count);
}

public static void demo1() { //求1000的阶乘中所有的零
BigInteger bi1 = new BigInteger("1");
for(int i = 1; i <= 1000; i++) {
BigInteger bi2 = new BigInteger(i+"");
bi1 = bi1.multiply(bi2); //将bi1与bi2相乘的结果赋值给bi1
}
String str = bi1.toString(); //获取字符串表现形式
int count = 0;
for(int i = 0; i < str.length(); i++) {
if('0' == str.charAt(i)) { //如果字符串中出现了0字符
count++; //计数器加1
}
}
System.out.println(count);
}

}

Test7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Test7 {

/**
* @param args
* 需求:求出1000的阶乘尾部零的个数,用递归做
* 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100...1000 1000 / 5 = 200
* 5 * 5 5 * 5 * 2 5 * 5 * 3 5 * 5 * 4 5 * 5 * 5 5 * 5 * 6 200 / 5 = 40
* 5 * 5 * 5 * 1 5 * 5 * 5 * 2 5 * 5 * 5 * 3 5 * 5 * 5 * 4 5 * 5 * 5 * 5 5 * 5 * 5 * 6 5 * 5 * 5 * 7 5 * 5 * 5 * 8
40 / 5 = 8
5 * 5 * 5 * 5 8 / 5 = 1
*/
public static void main(String[] args) {
System.out.println(fun(1000));
}

public static int fun(int num) {
if(num > 0 && num < 5) {
return 0;
}else {
return num / 5 + fun(num / 5);
}
}
}

Test8

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
public class Test8 {

/**
* @param args
* 约瑟夫环
* * 幸运数字
*/
public static void main(String[] args) {
System.out.println(getLucklyNum(8));
}

/*
* 获取幸运数字
* 1,返回值类型int
* 2,参数列表int num
*/
public static int getLucklyNum(int num) {
ArrayList<Integer> list = new ArrayList<>(); //创建集合存储1到num的对象
for(int i = 1; i <= num; i++) {
list.add(i); //将1到num存储在集合中
}

int count = 1; //用来数数的,只要是3的倍数就杀人
for(int i = 0; list.size() != 1; i++) { //只要集合中人数超过1,就要不断的杀
if(i == list.size()) { //如果i增长到集合最大的索引+1时
i = 0; //重新归零
}

if(count % 3 == 0) { //如果是3的倍数
list.remove(i--); //就杀人
}
count++;
}
return list.get(0);
}
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1210331079@qq.com

💰

Title:Java基础23-递归练习

Count:2.3k

Author:千 羽

Created At:2020-06-16, 12:21:56

Updated At:2020-11-27, 09:02:29

Url:https://nateshao.github.io/2020/06/16/Java%E5%9F%BA%E7%A1%8023-%E9%80%92%E5%BD%92%E7%BB%83%E4%B9%A0/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

donation.headline

// 底部音乐
//右上角Github图标