蓝桥杯冲刺-day01
马上就要蓝桥杯比赛了,慌得一批,搞点题目训练冲刺一下
所有题目都来源于AcWing,大家也可以去报名闫总的蓝桥杯特训课程哦,真的超赞的哦
AcWing 092 递归实现指数型枚举
分类: 递归与递推
题目描述
从 1~n 这 n 个整数中随机选取任意多个,输出所有可能的选择方案。
1≤n≤15
输入样例:
3
输出样例:
3
2
2 3
1
1 3
1 2
1 2 3
简单的分析一下下:输入就是简单的一个整数,输出是遍历所有的可能选择方案,方案是从一到n中任意选择多个
比如n=3,可以是选择1 2, 也可以是选择 1 3, 也可以是只选择1。
题目有一个数据范围,n<=15
可以用数组表示选择还是不选择 new int[16]; 0 表示还未选择, 1表示已经选择,2表示不选择
ok大概就是这样的思想,然后直接遍历即可,dfs从1到n这n个数字就ok,
代码
import java.util.*;
public class Main{
static int[] st = new int[16]; //0表示还未选,1表示选择,2表示不选择
static int n;
static void dfs(int x){
if(x>n){
for(int i=1;i<=n;i++){
if(st[i]==1){
System.out.print(i+" ");
}
}
System.out.println();
return; // 不要忘记了
}
//状态
//1. 这个,我选择
st[x] = 1;
dfs(x+1);
//2. 不选
st[x] = 2;
dfs(x+1);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
dfs(1);
}
}
知识点:遍历可以用一个数组看是否遍历
0 默认未选择
1 选择
2 不选择
AcWing 094 递归实现排列型枚举
题目描述
把 1~n 这 n 个整数排成一行后随机打乱顺序,输出所有可能的次序。
1≤n≤9
输入样例:
3
输出样例:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
简单分析:这和上一题很像,但是还是读题
1~n这n个数随机打乱输出,靠,这就是全排列嘛,对吧
这里st 数字是否被使用,
state存那个全排列
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class Main {
static final int N = 10;
static int n;
static int[] state = new int[N];
static boolean[] used = new boolean[N]; // true表示用过,false表示还未用过
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static void dfs(int u) throws IOException{
if(u > n){
for(int i=1;i<=n;i++){
bw.write(state[i]+" ");
}
bw.newLine();
bw.flush();
}
//枚举两个分支
for(int i=1;i<=n;i++){
if(!used[i]){
state[u] = i; //这个位置填上
used[i] = true;
dfs(u+1);
//回溯
state[u] = 0; //位置清空
used[i] = false;
}
}
}
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
dfs(1);
bw.close();
}
}
AcWing 717 简单斐波那契
题目描述
以下数列0 1 1 2 3 5 8 13 21 …被称为斐波纳契数列。
这个数列从第3项开始,每一项都等于前两项之和。
输入一个整数N,请你输出这个序列的前N项。
这题直接写,思路比较清晰
代码
import java.io.*;
import java.util.*;
public class Main{
static int N = 50;
static int[] f = new int[N];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n == 1) System.out.print(0);
if(n == 2) System.out.print(0 + " " + 1);
if(n>3){
f[1] = 0; System.out.print(0 +" ");
f[2] = 1;System.out.print(1 +" ");
for(int i = 3;i <= n;i++)
{
f[i] = f[i - 1] + f[i - 2];
System.out.print(f[i] +" ");
}
}
}
}
AcWing 1209 带分数
题目描述
100 可以表示为带分数的形式:100=3+69258/714
还可以表示为:100=82+3546/197
注意特征:带分数中,数字 1∼9 分别出现且只出现一次(不包含 0)。
类似这样的带分数,100 有 11 种表示法。
输入格式
一个正整数。
输出格式
输出输入数字用数码 1∼9 不重复不遗漏地组成带分数表示的全部种数。
1≤N<10^6
思路:全排列+检测的方法
代码
import java.util.Scanner;
/**
*
*
* 带分数
* @author vccyb
*
*/
/**
* 100 可以表示为带分数的形式:100=3+69258/714
还可以表示为:100=82+3546/197
注意特征:带分数中,数字 1∼9 分别出现且只出现一次(不包含 0)。
类似这样的带分数,100 有 11 种表示法。
*
*
* @author vccyb
*
*/
public class Main {
static final int N = 10;
static int target;
static int[] num = new int[N];
static boolean[] used = new boolean[N];
static int cnt;
static int calc(int l, int r){
int res = 0;
for(int i=l;i<=r;i++){
res = res*10+num[i];
}
return res;
}
static void dfs(int u){
//分成三段
// a b c
//
if(u==9){
for(int i=0;i<7;i++){
for(int j=i+1;j<8;j++){
int a = calc(0,i);
int b = calc(i+1,j);
int c = calc(j+1,8);
if(a*c+b == c * target){cnt++;}
}
}
return;
}
//继续搜索
for(int i=1;i<=9;i++){
if(!used[i]){
used[i] = true; //标记使用了
num[u] = i;
dfs(u+1);
used[i] = false; //还原
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
target = sc.nextInt();
dfs(0);
System.out.println(cnt);
}
}
AcWing 093 递归实现组合型枚举
题目描述
从 1~n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案。
输入格式
两个整数 n,m ,在同一行用空格隔开。
输出格式
按照从小到大的顺序输出所有方案,每行1个。
首先,同一行内的数升序排列,相邻两个数用一个空格隔开。
其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面(例如1 3 5 7排在1 3 6 8前面)。
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static List<Integer> list = new ArrayList<Integer>();
static void dfs(int x, int n, int m){
//x表示整数x,n表示个整数,m表示长度
//保证就m个数字
if(list.size() > m || list.size() + (n - x + 1) < m) return;
if(x == n+1){
//全部选择完毕
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
System.out.println();
return;
}
//选择
list.add(x);
dfs(x+1,n,m);
list.remove(list.size()-1); //回溯
dfs(x+1,n,m); //不选的情况
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
sc.close();
dfs(1,n,m);
}
}

浙公网安备 33010602011771号