递归与递推

递归 

92. 递归实现指数型枚举 - AcWing题库

import java.util.*;

public class Main{
    static int N = 16, n;
    static int[] st = new int[N];//st[x] 等于0表示还没考虑到它,等于1表示选它,等于2表示不选它
    
    public static void dfs(int u){
        if(u > n){
            for(int i = 1; i <= n; i ++){
                if(st[i] == 1) System.out.print(i + " ");
            }
            System.out.println();
            return;//一定要记得返回return
        }
        
        //不选的情况
        st[u] = 2;
        dfs(u + 1);
        st[u] = 0;//恢复现场
        
        //选的情况
        st[u] = 1;
        dfs(u + 1);
        st[u] = 0;//恢复现场
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        dfs(1);//由于数字范围是1到n,所以这里的dfs要从1开始,不从0开始
    }
}

94. 递归实现排列型枚举 - AcWing题库

import java.util.*;

public class Main{
    static int N = 10, n;
    static int[] path = new int[N];//记录每一层的数
    static boolean[] st = new boolean[N];//用来判断这个数是否用过
    
    public static void dfs(int u){
        if(u > n){
            for(int i = 1; i <= n; i ++){
                System.out.print(path[i] + " ");
            }
            System.out.println();
        }
        
        for(int i = 1; i <= n; i ++){
            if(!st[i]){
                path[u] = i;
                st[i] = true;
                //递归下一层
                dfs(u + 1);
                //恢复现场
                path[u] = 0;
                st[i] = false;
            }
        }
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        dfs(1);
    }
}

93. 递归实现组合型枚举 - AcWing题库

限制:从小到大排序(局部限制),保证每次加的数都比前一个数大

import java.util.*;

public class Main{
    static int N = 30, n, m;
    static int[] state = new int[N];
    
    public static void dfs(int u, int start){
        if(u - 1 + n - start + 1 < m) return;//剪枝
        if(u == m + 1){
            for(int i = 1; i <= m; i ++){
                System.out.print(state[i] + " ");
            }
            System.out.println();
        }
        
        for(int i = start; i <= n; i ++){
            state[u] = i;
            dfs(u + 1, i + 1);
            state[u] = 0;//恢复现场
        }
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        
        dfs(1, 1);//从第一个位置开始,当前可以从1开始选
    }
}

1209. 带分数 - AcWing题库

import java.util.*;
import java.io.*;

public class Main{
    static int N = 20;
    static int n, res;
    static boolean[] st = new boolean[N], cpy = new boolean[N];
    
    public static void dfs_a(int u, int a){
        if(a >= n) return;//大于n直接返回
        
        if(a > 0) dfs_c(u, a, 0);//枚举c的所有排列方案
        
        for(int i = 1; i <= 9; i ++){
            if(!st[i]){
                st[i] = true;
                dfs_a(u + 1, a * 10 + i);
                st[i] = false;//恢复现场
            }
        }
    }
    
    public static void dfs_c(int u, int a, int c){
        if(u > 9) return;
        
        if(check(a, c)) res ++;
        
        for(int i = 1; i <= 9; i ++){
            if(!st[i]){
                st[i] = true;
                dfs_c(u + 1, a, c * 10 + i);
                st[i] = false;//恢复现场
            }
        }
    }
    
    public static boolean check(int a, int c){
        long b = (long) (n - a) * c;
        
        cpy = Arrays.copyOf(st, N);
        
        while(b > 0){
            int t = (int)(b % 10);
            b = b / 10;
            
            if(t == 0 || cpy[t]) return false;//如果t为0,或者被用过
            
            cpy[t] = true;
        }
        
        //检查是否有遗漏
        for(int i = 1; i <= 9; i ++){
            if(!cpy[i]) return false;
        }
        
        return true;
    }
    
    public static void main(String[] args)throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        
        dfs_a(0, 0);//一开始a和c共用0个数,a为0
        
        System.out.print(res);
    }
}

递推

717. 简单斐波那契 - AcWing题库

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int[] f = new int[46];
        f[1] = 0;
        f[2] = 1;
        
        int n = sc.nextInt();
        for(int i = 3; i <= n; i ++){
            f[i] = f[i - 1] + f[i - 2];
        }
        
        for(int i = 1; i <= n; i ++){
            System.out.print(f[i] + " ");
        }
    }
}
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a = 0, b = 1;
        for(int i = 1; i <= n; i ++){
            System.out.print(a + " ");
            int c = a + b;
            a = b;
            b = c;
        }
    }
}

95. 费解的开关 - AcWing题库

        1.顺序可以任意

        2.每个格子最多按一次

        每一行开关的操作都被前一行的开关状态所决定 

import java.util.*;

public class Main{
    static int N = 6;
    static int[][] g = new int[N][N];
    static int[][] backup = new int[N][N];
    static int[] dx = {-1, 0, 1, 0, 0}, dy = {0, 1, 0, -1, 0};
    
    public static 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 >= 5 || b >= 5) continue;
            g[a][b] ^= 1;
        }
    }
    
    public static int dfs(){
        int ans = 0x3f3f3f3f;
        
        //备份数组,用于多组测试(32)
        for(int i = 0; i < 5; i ++){
            for(int j = 0; j < 5; j ++){
                backup[i][j] = g[i][j];
            }
        }
        
        for(int k = 0; k < 1 << 5; k ++){
            int res = 0;//本次循环的结果
            
            //第一行
            for(int j = 0; j < 5; j ++){
                if((k >> j & 1) == 0){
                    res ++;
                    turn(0, j);
                }
            }
            
            for(int i = 0; i < 4; i ++){//第一行到第四行
                for(int j = 0; j < 5; j ++){//第一个数到第五个数
                    if(g[i][j] == 0){
                        res ++;
                        turn(i + 1, j);
                    }
                }
            }
            
            boolean flag = true;
            for(int j = 0; j < 5; j ++){//判断最后一行是否全为1
                if(g[4][j] == 0){
                    flag = false;
                    break;
                }
            }
            
            if(flag) ans = Math.min(ans, res);
            for(int i = 0; i < 5; i ++){
                for(int j = 0; j < 5; j ++){
                    g[i][j] = backup[i][j];//复原数组,便于下次循环
                }
            }
        }
        if(ans > 6) return -1;
        else return ans;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while(T -- > 0){
            for(int i = 0; i < 5; i ++){
                String s = sc.next();
                for(int j = 0; j < 5; j ++){
                    g[i][j] = s.charAt(j) - '0';//转化为int类型
                }
            }
            System.out.println(dfs());
        }
    }
}

1208. 翻硬币 - AcWing题库

次数和顺序没有关系 

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        int res = 0;
        char[] A = a.toCharArray();
        char[] B = b.toCharArray();
        
        for(int i = 0; i < a.length(); i ++){
            if(A[i] != B[i]){
                res ++;
                if(B[i + 1] == '*') B[i + 1] = 'o';
                else B[i + 1] = '*';
            }
        }
        
        System.out.print(res);
    }
}

 

116. 飞行员兄弟 - AcWing题库

暴力枚举所有方案(0 ~ 2^16 - 1),按照该方案对所有开关进行操作,然后判断,记录方案 

import java.util.*;

class PII{
    int x, y;
    public PII(int x, int y){
        this.x = x;
        this.y = y;
    }
}

public class Main{
    static int N = 5;
    static char[][] g = new char[N][N];
    static char[][] backup = new char[N][N];
    //找到每个位置的编号
    public static int get(int x, int y){
        return 4 * x + y;
    }
    
    //改变同一行同一列元素的状态
    public static void turn_all(int x, int y){
        for(int i = 0; i < 4; i ++){
            turn_one(x, i);
            turn_one(i, y);
        }
        turn_one(x, y);
    }
    
    //具体的改变函数
    public static void turn_one(int x, int y){
        if(g[x][y] == '+') g[x][y] = '-';
        else g[x][y] = '+';
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        //输入数据
        for(int i = 0; i < 4; i ++){
            String s = sc.next();
            for(int j = 0; j < 4; j ++){
                g[i][j] = s.charAt(j);
            }
        }
        
        List<PII> res = new ArrayList<>();//用来记录最终路径
        for(int k = 0; k < 1 << 16; k ++){
            List<PII> temp = new ArrayList<>();
            
            //备份
            for(int i = 0; i < 4; i ++){
                backup[i] = g[i].clone();
            }
            
            for(int i = 0; i < 4; i ++){
                for(int j = 0; j < 4; j ++){
                    if((k >> get(i, j) & 1) == 1){
                        temp.add(new PII(i, j));//记录路径
                        turn_all(i, j);//改变同一行同一列元素的状态
                    }
                }
            }
            
            boolean flag = true;//检查开关是否都打开了
            for(int i = 0; i < 4; i ++){
                for(int j = 0; j < 4; j ++){
                    if(g[i][j] == '+'){
                        flag = false;
                    }
                }
            }
            
            if(flag){
                if(res.size() == 0 || res.size() > temp.size()) res = temp;//如果还没有合法值,或者当前值比合法值小
            }
            
            //复原
            for(int i = 0; i < 4; i ++){
                g[i] = backup[i].clone();
            }
        }
        
        System.out.println(res.size());
        for(PII i: res){
            System.out.println((i.x + 1) + " " + (i.y + 1));
        }
    }
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/452253.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

蓝桥·算法双周赛|第七场分级赛——小白入门赛

&#x1f525;博客介绍&#xff1a; 27dCnc &#x1f3a5;系列专栏&#xff1a; <<数据结构与算法>> << 算法入门>> << C项目>> &#x1f3a5; 当前专栏: << 算法入门>> 专题 : 数据结构帮助小白快速入门算法 &#x1f4…

蓝鲸作业平台升级openssh执行方案分享

本文来自腾讯蓝鲸智云社区用户&#xff1a;AK47 蓝鲸的运维系统在我们单位使用已经快四个年头了&#xff0c;从刚开始的5到现在最新的7.1都有部署、测试、验证和使用。在实际的使用过程中&#xff0c;给我们运维提供了非常大的帮助。其中有一个场景分享给大家。这个场景是关于o…

NVidia NX 中 ROS serial软件包的安装

自己装的ROS是noetic版本&#xff0c;受限于网络&#xff0c;直接用命令安装串口包不行。于是手动安装了一次。 1 下载源码 git clone https://github.com/wjwwood/serial.git 或者直接在浏览器里面输入 https://github.com/wjwwood/serial.git 2 解压 然后在serial&#xf…

LeetCode108题:将有序数组转换为二叉搜索树(python3)

一个容易想到的思路&#xff1a;使用 nums 中最靠近中心的位置作为整棵 BST 的根节点&#xff0c;确保左右子树节点数量平衡。随后递归构造 nums 中下标范围为 [0,mid−1]作为左子树&#xff0c;递归构造 nums 中下标范围为 [mid1,n−1]作为右子树。 # Definition for a binar…

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的田间杂草检测系统(深度学习模型+UI界面+Python代码+训练数据集)

摘要&#xff1a;开发用于田间杂草识别的系统对提高农业运营效率和提升作物产出至关重要。本篇文章详尽阐述了如何应用深度学习技术开发一个用于田间杂草识别的系统&#xff0c;并附上了完备的代码实现。该系统基于先进的YOLOv8算法&#xff0c;并对比了YOLOv7、YOLOv6、YOLOv5…

保护数字前沿:有效的威胁暴露管理

人工智能技术正在从根本上改变网络安全领域的方向。仅 2023 年&#xff0c;全球企业预计将在人工智能上花费 1027.8 亿美元&#xff0c;以阻止网络安全威胁。 人工智能 (AI)在增强网络安全措施方面发挥着关键作用&#xff0c;因为它能够快速分析大量数据并识别可能表明潜在威胁…

Weblogic 常规渗透测试环境

测试环境 本环境模拟了一个真实的weblogic环境&#xff0c;其后台存在一个弱口令&#xff0c;并且前台存在任意文件读取漏洞。分别通过这两种漏洞&#xff0c;模拟对weblogic场景的渗透。 Weblogic版本&#xff1a;10.3.6(11g) Java版本&#xff1a;1.6 弱口令 环境启动后…

读取Excel的封装方法

步骤&#xff1a; 1、我们需要将得到的结果存储到list集合中&#xff0c;所以实例化一个ArrayList类 List<CaseInfo> list new ArrayList<CaseInfo>();//实例化一个类 常规下在list里添加内容即可 不再是字符串类&#xff0c;是caseinfo用例 list.add里添加casei…

【位运算】【脑筋急转弯】2749. 得到整数零需要执行的最少操作数

作者推荐 视频算法专题 本文涉及知识点 2749. 得到整数零需要执行的最少操作数 给你两个整数&#xff1a;num1 和 num2 。 在一步操作中&#xff0c;你需要从范围 [0, 60] 中选出一个整数 i &#xff0c;并从 num1 减去 2i num2 。 请你计算&#xff0c;要想使 num1 等于 …

常用的gpt网站

ChatGPT是一款基于人工智能技术的对话型AI助手&#xff0c;能够进行自然语言交互并提供个性化的对话服务。通过先进的深度学习模型&#xff0c;ChatGPT能够理解用户输入的文本&#xff0c;并生成有逻辑、连贯性的回复。它可以回答各种问题、提供建议、分享知识&#xff0c;还能…

Zookeeper搭建

目录 前言 初了解Zookeeper 搭建 准备 配置Zookeeper 前言 今天来介绍Zookeeper的搭建&#xff0c;其实Zookeeper的搭建很简单&#xff0c;但是为什么还要单独整一节呢&#xff0c;这就不得不先了解Zookeeper有什么功能了&#xff01;而且现在很火的框架也离不开Zookeepe…

来来来, SAP BTP下使用CAP来生成PostgreSQL应用(一): Node.js篇

前言 SAP云应用程序编程模型(CAP)是一个语言、库和工具框架&#xff0c;用于构建企业级服务和应用程序。它引导开发人员沿着一条经过验证的最佳实践的“黄金之路”前进&#xff0c;并为反复出现的任务提供大量开箱即用的解决方案。 我们这就来看看SAP BTP的CAP到底提供了哪些…

有来团队后台项目-解析5

一、 husky 安装 pnpm install -D husky生成husky 配置文件 如果文件中有.git文件&#xff0c;那么直接执行 npx husky-init如果没有&#xff0c;那么先执行git init 结果&#xff1a; PS F:\company_project\demo\youlahoutaijiexi\vite-project> git init Initializ…

【DDR】DDR4学习记录

这里以美光DDR4芯片 MT40A512M16HA-075E datasheet 为例&#xff0c;说明DDR4存储器的原理及仿真。   根据开发板手册ug1302&#xff0c;在vcu128&#xff08;xcvu37p&#xff09;开发板上&#xff0c;共具有5块DDR4芯片&#xff0c;在数据信号上4块DDR4具有16位数据线&#…

【HomeAssistant新版文件管理器】

【HomeAssistant新版文件管理器】 1. 前言2. 地址3. 安装4. 使用方法5. 总结欢迎大家阅读2345VOR的博客【Home Assistant 之QQ邮箱推送提醒】🥳🥳🥳2345VOR鹏鹏主页: 已获得CSDN《嵌入式领域优质创作者》称号🎉🎉、阿里云《arduino专家博主》👻👻👻,座右铭:…

蓝桥杯--日期统计

目录 一、题目 二、解决代码 三、代码分析 ​四、另一种思路 五、关于set文章推荐 一、题目 二、解决代码 #include <bits/stdc.h> using namespace std; int main() {int arr[100] { 5,6,8,6,9,1,6,1,2,4,9,1,9,8,2,3,6,4,7,7,5,9,5,0,3,8,7,5,8,1,5,8,6,1,8,3,0,…

第五节:使用SMB开发WebSocket通信

一、概述 本节主要讲解在SMB中如何进行websocket快速开发&#xff0c;实现客户端连接、关闭、消息通讯等功能。 示例下载&#xff1a;https://download.csdn.net/download/lllllllllluoyi/88949743 二、创建WebSocket服务器 1、在csdnProject工程中新建一个消息流。 添加W…

命名空间多线程计时(C++基础)

命名空间 不要在头文件内使用using namespace&#xff0c;一定要确保实在一个足够小的作用域下使用&#xff0c;在哪个范围内&#xff0c;比如函数、if语句等&#xff0c;但一定不要在头文件中使用&#xff01;&#xff01;&#xff01; 上述示例中&#xff0c;会调用orange中…

windows系统图标变白设置

我们在使用系统的时候&#xff0c;通常会在桌面创建图标&#xff0c;有时候桌面图标过多&#xff0c;整理图标放在新建文件夹的时候&#xff0c;图标变白&#xff0c;通常情况下都是缓存问题&#xff0c;这里也是删除缓存解决演示系统&#xff1a;windows11 1显示图标缓存目录 …

用Python进行机器学习:Scikit-learn的入门与实践【第126篇—Scikit-learn的入门】

用Python进行机器学习&#xff1a;Scikit-learn的入门与实践 随着机器学习在各个领域的广泛应用&#xff0c;Python成为了一个备受欢迎的机器学习工具之一。在众多机器学习库中&#xff0c;Scikit-learn因其简单易用、功能强大而备受青睐。本文将介绍Scikit-learn的基本概念&am…