🙏🤗距离【第十三届蓝桥杯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的阶乘
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
}
}