0
点赞
收藏
分享

微信扫一扫

【蓝桥杯省赛】冲刺练习题【递归】倒计时【11】天

闲云困兽 2022-03-30 阅读 56

 🙏🤗距离【第十三届蓝桥杯4月9日省赛】仅剩11天】🤗🙏

📋今日题型:【递归】📋

⭐️🤗循环是一切暴力的基础,暴力基础,转起来。🤗⭐️

🤗国一镇楼🤗

📋比赛题目与分数比例📋

⭐️🤗刷题安排🤗⭐️

日期题目类型题目数量
3月25日循环6
3月26日超大数6
3月27日数组6
3月28日枚举6
3月29日递归6
3月30日绘图6
3月31日深搜广搜5
4月1日动态规划5
4月2日填空题5
4月3日数学公式:查询准考证5
4月4日第十届省赛题10
4月5日第十一届省赛题10
4月6日第十二届省赛1套题10
4月7日第十二届省赛2套题10
4月8日经典题目练习8
4月9日9点考试

目录

1、求10的阶乘

2、斐波那契数组

3、排列问题

4、取球问题

5、李白打酒

6、对数组进行全排列

附加题:对字符串全排列


1、求10的阶乘

package test;

public class demo {
	public static void main(String[] args) {
		int s = f(10);
		System.out.println(s);
	}

	public static int f(int n) {
		if (n <= 1)
			return 1;
		return f(n - 1) * n;
	}
}

2、斐波那契数组

package test;

public class demo {
	public static void main(String[] args) {
		System.out.println(f(10));
	}

	public static int f(int n) {
		if (n == 0) {
			return 0;
		}
		if (n == 1) {
			return 1;
		}
		return f(n - 1) + f(n - 2); // 表示n的前一项,加上n的前前一项
	}
}

3、排列问题

package test;

public class demo {
	public static void main(String[] args) {
		System.out.println(f(3, 2));
	}

	public static int f(int m, int n) {
		if (m == 0 || n == 0)
			return 1;
		return f(m - 1, n) + f(m, n - 1); // 核心思想
	}
}

4、取球问题

利用公式:p(n,m)=n!/(n-m)! 

package test;

public class demo {
	public static void main(String[] args) {
		// 在10个球中,取3个(不放回)
		System.out.println(f(10)/f(10-3));
	}

	public static int f(int n) {
		if (n <= 1)
			return 1;
		return f(n - 1) * n;
	}
}

这种方法效率很高。

5、李白打酒

话说大诗人李白,一生好饮。幸好他从不开车。
一天,他提着酒壶,从家里出来,酒壶中有酒2斗。
他边走边唱:
无事街上走,提壶去打酒。
逢店加一倍,遇花喝一斗。
这一路上,他一共遇到店5次,遇到花10次,
已知最后一次遇到的是花,他正好把酒喝光了。
请你计算李白遇到店和花的次序,可以把遇店记为a,遇花记为b。则:babaabbabbabbbb 就是合理的次序。像这样的答案一共有多少呢?请你计算出所有可能方案的个数(包含题目给出的)。

package test;

public class demo {
	public static void main(String[] args) {
		System.out.println(f(2, 5, 9, 15));
	}

	public static int f(int a, int b, int c, int d) {
		if (a == 0 || d == 0) {
			return 0;
		}
		if (a == 1 && b == 0 && c == 0 && d == 1) {
			return 1;
		}
		return f(a * 2, b - 1, c, d - 1) + f(a - 1, b, c - 1, d - 1);
	}
}

6、对数组进行全排列

package test;

import java.util.ArrayList;

public class demo {
	static ArrayList<Integer> l = new ArrayList<Integer>();
	static boolean[] b = new boolean[10];
	static int sum = 0;

	static void f(int[] a, int end) {
		if (end == a.length) {
			System.out.println(l);
			sum++;
		} else {
			for (int i = 0; i < a.length; i++) {
				if (!b[i]) {
					l.add(a[i]);
					b[i] = true;
					f(a, end + 1);
					l.remove(l.size() - 1);
					b[i] = false;
				}
			}
		}
	}

	public static void main(String[] args) {
		int[] a = { 0, 1, 2 };
		f(a, 0);
		System.out.println(sum);
	}
}

附加题:对字符串全排列

package Action;
 
public class test {
	public static void main(String[] args) {
		String s = "我爱你";
		char[] array = s.toCharArray();
		int count=0;
		//"字符串不能完全相同"判断
		for (int i = 0; i < array.length-1; i++) {
			if(array[i]==array[i+1]) {
				count++;
			}
		}
		if(count==array.length-1) {
			System.out.println(s);
			return;
		}
		// 通过字符处理
		f(s.toCharArray(), 0, s.length() - 1);
	}
 
	public static void f(char[] s, int from, int to) {
		if (from == to) {//递归终止条件
			System.out.println(s);//打印结果
		} else {
			// 从from开始,循环到to结束
			for (int i = from; i <= to; i++) {
				change(s, i, from); //交换前缀,作为结果中的第一个元素,然后对剩余的元素全排列
				f(s, from + 1, to); //递归调用,缩小问题的规模,form每次+1
				change(s, from, i); //换回前缀,复原字符数组
			}
		}
	}
 
	/**
	 * 用于交换的
	 * 
	 * @param s
	 * @param from
	 * @param to
	 */
	public static void change(char[] s, int from, int to) {
		char temp = s[from];// 定义第三方temp,获取from
		s[from] = s[to];// 从to交换到from
		s[to] = temp;// 从temp还给to
	}
}
举报

相关推荐

0 条评论