寒假刷题-递归与递推

news/2024/7/20 20:18:55 标签: 算法, 深度优先, 蓝桥杯

寒假刷题

92. 递归实现指数型枚举

image-20240115182149547

image-20240115182203169

解法1递归

使用递归对每一个坑位进行选择,每个坑位有两种选择,填或者不填,使用st数组来记录每个坑位的状态,u来记录已经有多少坑位有了选择。

image-20240115194744258

每个坑位有2钟选择,n个坑位的复杂度就是2的n次方。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 16;
int n;
int st[N];
void dfs(int u){
    if(u>n)
    {
        for(int i = 1;i<=n;i++)
        {
            if(st[i]==1)printf("%d ",i);
        }
        printf("\n");
        return;
    }
    st[u] = 1;
    dfs(u+1);
    st[u]=0;
    dfs(u+1);
}
int main(){
    scanf("%d",&n);
    dfs(1);
    return 0;
}

坑位的下标和这个坑位的数字是存在对应关系的,所以可以用一个u来控制递归的出口。我们只关心u位置是否有数字。
st[u] = 1;
dfs(u+1);
这两句相当于是将这个位置画对勾,然后跳到下一个位置进行选择
st[u]=0;
dfs(u+1);
这两句就是这个位置不填数进入下一轮,顺便回到dfs之前的状态。(这题无所谓)

解法2二进制压缩

1-n的所有整数排列的方案可以看作一个二进制序列,例如1-3的排列中,1 3就对应二进制101。有数字用1表示,没有数字用0表示。
1-n共有2的n次方钟方案。将所有方案数枚举,然后判断位数是否是1。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int main()
{
    scanf("%d",&n);
    for(int i = 0;i < 1<<n;i ++)
    {
        for(int j = 0;j < n;j ++)
        {
            if(i >> j & 1)printf("%d ",j + 1);
        }
        printf("\n");
    }
    return 0;
}

94. 递归实现排列型枚举

这题就是一个全排列问题,和上一题的区别是很明显的。上一个的每个坑位的数字是固定的,可能有或没有,这个题的每个坑位的数是不固定的,且必须有。这个题需要使用st记录是否使用过。这个st和上一个题的st代表的意义不一样。

使用循环来进行dfs。循环从1开始,到n结束。通过st[i]可以知道数字i是否被使用过。如果没被使用过就使用i,然后进入下一层搜索。使用后一定要恢复现场。

image-20240115203650666

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 10;
int m,st[N],ans[N];
int n;
void dfs(int x)
{
    if(x > n) 
    {
       for(int i = 1;i <= n;i ++)
       {
           printf("%d ",ans[i]);
       }
       printf("\n");
    }
 
    for(int i = 1;i <=n;i ++)
    {
        if(!st[i])
        {
            st[i] = 1;
            ans[x] = i;
            dfs(x+1);
            st[i] = 0;
        }
    }
}
int main()
{
    scanf("%d",&n);
    dfs(1);
    return 0;
}

717. 简单斐波那契

image-20240115205913881

使用递推来进行求解,通过观察可以发现这个数列的第n项只与n-1和n-2项有直接关系,所以使用三个变量a b fn,依次向后轮转。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
    int n,fn;
    scanf("%d",&n);
    int a = 0,b = 1;
    for(int i = 1;i <= n;i ++)
    {
        cout<<a<<" ";
        fn = a+b;
        a=b,b=fn;
    }
}

95. 费解的开关

image-20240115210215243

image-20240115210246482

image-20240115210312290

样例

image-20240115210417313

改变右上角的开关

image-20240115210526298

image-20240115210552593

两步即可让所有的灯变亮。

观察题意可以发现能影响灯本身的除了灯自己还有灯上下左右的灯,可以枚举第一行灯的32种按法,记得备份原数组,然后从第一行按到第四行,第i行可以通过第i+1行的灯来控制,遍历完第四行后,看看第五行还有没有灭的灯,如果有的话,那这个方案就是不可行的。因为没有第六行来控制第五行。如果第五行全亮,那这个就是可以调,判断一下和ans哪个更小。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 6;
char g[N][N],backup[N][N];
int n;
int dx[] = {1,-1,0,0,0};
int dy[] = {0,0,-1,1,0};
void turn(int x,int y){
    for(int i = 0;i < 5;i ++)
    {
        int a = x + dx[i];
        int b = y + dy[i];
        if(a < 0 || b < 0 || a > 4 || b > 4)continue;
        g[a][b] ^= 1;
    }
}
int main(){
    scanf("%d",&n);
    while(n --)
    {
        for(int i = 0;i < 5;i ++)scanf("%s",g[i]);
        int ans = 10;
        for(int op = 0;op < 32;op ++)
        {
            memcpy(backup,g,sizeof g);
            int step = 0;
            for(int i = 0;i < 5;i ++)
            {
                if(op >> i & 1)
                {
                    turn(0,i);
                    step ++;
                }
            }
            for(int i = 0;i < 4;i ++)
            {
                for(int j = 0;j < 5;j ++)
                {
                    if(g[i][j] == '0')
                    {
                        turn(i+1,j);
                        step ++;
                    }
                }
            }
            int drak = 0;
            for(int j = 0;j < 5;j ++)
            {
                if(g[4][j] == '0')
                {
                    drak = 1;
                    break;
                }
            }
            if(drak == 0)ans = min(ans,step);
            memcpy(g,backup,sizeof g);
        }
        if(ans > 6)ans = -1;
        cout << ans <<endl;
    }
    return 0;
}

93. 递归实现组合型枚举

image-20240115212247504

image-20240115212301909

这题与全排列的区别就是字典序,只需要在判断是否使用过的时候加上一个判断,条件是当前的i是否大于ans数组的的最后一个元素,大于往里面添加,小于直接就跳过即可。ans数组初始化时要将元素变为-1,否则开头将无法添加。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 26;
int n,m;
int ans[N] = {-1},st[N]={0};
void dfs(int c,int m){
    if(c > m)
    {
        for(int i = 1;i <= m; i ++)
        {
            cout << ans[i] << " ";
        }
        cout<<endl;
        return;
    }
    for(int i = c ;i <= n; i ++)
    {
        if(!st[i]&&i > ans[c-1])
        {
          
          ans[c] = i;
          st[i] = 1;
          dfs(c + 1,m);
          st[i] = 0;
        }
       
    }
}
int main(){
    scanf("%d%d",&n,&m);
    dfs(1,m);
    return 0;
}

1209. 带分数

image-20240115212751409

image-20240115212807361

题意是
n = a + b / c n = a + b/c n=a+b/c
等式两边同时×c
c ∗ n = c ∗ a + b c*n = c*a+b cn=ca+b
通过dfs枚举a和c,然后计算出b,然后遍历st数组看看是否b的每一位都没有被用到。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 30;
int st[N],backup[N];
int n,ans,test;
bool check(int a,int c)
{
    long long b = (long long) n * c -a * c;
    
    if(!a || !b || !c)return false;
   
    memcpy(backup,st,sizeof st);

    while(b)
    {
        int ge = b % 10;
        if(ge == 0 || backup[ge])return false;
        backup[ge] = 1;
        b /= 10;
    }
       
    for(int i = 1;i <= 9;i ++)
    {
        if(backup[i] == 0) return false;
    }
    return true;
}
void dfs_c(int a,int c)
{
    
    if(check(a,c))ans ++;
    for(int i = 1; i <= 9; i ++)
    {
        if(st[i] == 0)
        {
            st[i] = 1;
            dfs_c(a,c*10 + i);
            st[i] = 0;
        }
    }
}
void dfs_a(int a)
{
    
    if(a > n)return;
    if(a)dfs_c(a,0);
    for(int i = 1;i <= 9; i ++)
    {
        if(st[i] == 0)
        {
            st[i] = 1;
            dfs_a(a*10 + i);
            st[i] = 0;
        }
    }
}
int main()
{
    scanf("%d",&n);
    dfs_a(0);
    cout << ans << endl;
    return 0;
}

116. 飞行员兄弟

和费解的开关类似,只不过这个题的数量比较下,所以枚举所有行的全部可能,共65536种,对每一种方案进行操作,记录最少的方案数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define x first
#define y second
using namespace std;

typedef pair<int,int> PII;
const int N = 5;
char st[N][N],backup[N][N];
vector <PII> ans;
int get(int x,int y)
{
    return x*4 + y;
}
void turn(int x,int y)
{
    if(st[x][y]=='+')st[x][y]='-';
    else st[x][y]='+';
}
void turnall(int x,int y)
{
    for(int i = 0;i < 4;i ++)
    {
        turn(x,i);
        turn(i,y);
    }
    turn(x,y);
}
int main()
{
    for(int i = 0;i < 4;i ++)
    {
        scanf("%s",st[i]);
    }
    for(int i = 0;i < 1 << 16;i ++)
    {
        vector <PII> step;
        memcpy(backup,st,sizeof st);
        for(int x = 0;x < 4;x ++)
        {
            for(int y = 0;y < 4;y ++)
            {
                if(i >> get(x,y) & 1)
                {
                    turnall(x,y);
                    step.push_back({x,y});
                }
            }
        }
        int dark = 0;
        for(int x = 0;x < 4;x ++)
        {
            for(int y = 0;y < 4;y ++)
            {
                if(st[x][y]=='+')
                {
                   dark = 1;
                   break;
                }
            }
        }
        if(dark == 0)
        {
            if(ans.empty()||ans.size() > step.size()) ans = step;
        }
         memcpy(st,backup,sizeof st);
    }
    
    cout << ans.size() << endl;
    for(auto p:ans)
    {
        cout << p.x+1<<" "<<p.y+1<<endl;
    }
    return 0;
}

1208. 翻硬币

有个初始态有个结束态。将初始态与结束态一一对比,遇到不一样的就将移动次数加一并且变换硬币状态。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int ans;
string start,aim;
void turn(int i)
{
    if(start[i]=='*')start[i] = 'o';
    else start[i] = '*';
}
int main()
{
    cin>>start>>aim;
    for(int i = 0;i < start.size() - 1;i ++)
    {
        if(start[i]!=aim[i])turn(i+1),ans++;
        
    }
    cout << ans <<endl;
    return 0;
}

http://www.niftyadmin.cn/n/5334774.html

相关文章

科普:大语言模型中的量化是什么意思?

大语言模型是指能够处理大规模文本数据的深度学习模型&#xff0c;例如GPT-3、BERT等。这些模型通常有数十亿甚至数百亿个参数&#xff0c;占用大量的存储空间和计算资源。为了提高这些模型的效率和可移植性&#xff0c;一种常用的方法是模型量化。 1&#xff1a;什么是模型量化…

C#: form 窗体的各种操作

说明&#xff1a;记录 C# form 窗体的各种操作 1. C# form 窗体居中显示 // 获取屏幕的宽度和高度 int screenWidth Screen.PrimaryScreen.Bounds.Width; int screenHeight Screen.PrimaryScreen.Bounds.Height;// 设置窗体的位置 this.StartPosition FormStartPosition.M…

普兰资产(PLAN B KRYPTO ASSETS):Schutz AI 公链引领数字资产新时代

比特B ETF是金融技术革命的起始 普兰资产&#xff08;PLAN B KRYPTO ASSETS&#xff09;执行长Jonah Fischer指出&#xff0c;比特B ETF 仅是迈向金融领域技术革命的首个阶段。他认为比特B现货 ETF 提供了投资者接触年轻且具有风险性的资产的途径&#xff0c;但他强调区块链技术…

pyDAL一个python的ORM(终) pyDAL的一些性能优化

一、大批量插入数据 对于 大量数据插入时&#xff0c;虽然pyDAL也手册中有个方法&#xff1a;bulk_insert()&#xff0c;但是手册也说了&#xff0c;虽然方法上是一次可以多条数据&#xff0c;如果后端数据库是关系型数据库&#xff0c;他转换为SQL时它是一条一条的插入的&…

第16章_网络编程拓展练习(TCP编程,UDP编程)

文章目录 第16章_网络编程拓展练习TCP编程1、学生与老师交互2、查询单词3、拓展&#xff1a;查询单词4、图片上传5、拓展&#xff1a;图片上传6、多个客户端上传文件7、群聊 UDP编程8、群发消息 第16章_网络编程拓展练习 TCP编程 1、学生与老师交互 案例&#xff1a;客户端模…

2024美赛数学建模思路 - 案例:异常检测

文章目录 赛题思路一、简介 -- 关于异常检测异常检测监督学习 二、异常检测算法2. 箱线图分析3. 基于距离/密度4. 基于划分思想 建模资料 赛题思路 &#xff08;赛题出来以后第一时间在CSDN分享&#xff09; https://blog.csdn.net/dc_sinor?typeblog 一、简介 – 关于异常…

初始Spring(适合新手)

一、Spring核心概念&#xff08;IOC&#xff09; 控制反转IOC:Inversion of control 控制对象产生的权利反转到spring ioc 依赖注入DI:Dependency injection 依赖spring ioc注入对象 最少jar包&#xff1a; spring-beans-.jar spring-context-.jar spring-core-.jar spring-ex…

1、前端项目初始化(vue3)

1、安装npm 安装npm&#xff0c;&#xff08;可以用nvm管理npm版本&#xff09;npm安装需要安装node.js&#xff08;绑定销售&#xff1f;&#xff09;而使用nvm就可以很方便的下载不同版本的node&#xff0c;这里是常用命令 1、nvm install &#xff1a;安装指定版本的Node.…