2-1基础算法-枚举/模拟

文章目录

  • 1.枚举
  • 2.模拟

1.枚举

[例1] 特别数的和
在这里插入图片描述
评测系统

#include <iostream>
using namespace std;
bool pa(int x) {
    while (x) {
        if (x % 10 == 2 || x % 10 == 1 || x % 10 == 0 || x % 10 == 9) {
            return true;
        }
        else {
            x = x / 10;
        }
    }
    return false;
}
int main()
{
    int sum=0;
    int m;
    cin >> m;
    for (int i = 1; i <= m; i++) {
        if (pa(i)) {
            sum += i;
        }
    }
    cout << sum;
    return 0;
}

[例2] 反序数
在这里插入图片描述
评测系统

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int a, b, c;
    cin >> a >> b >> c;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        if (i % a != 0 && i % b != 0 && i % c != 0) {
            sum++;
        }
    }
    cout << sum;
    return 0;
}

[例3] 找到最多的数
在这里插入图片描述
评测系统

#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    int num = n * m;
    map<int,int> mp;
    vector<int> a(num);//记录去重后的序列
    while (num--) {
        int x;
        cin >> x;
        if (!mp.count(x)) {
            a.push_back(x);
        }
        mp[x]++;
    }
    for (const auto& x:a) { //也可以不使用vector,每次从map中取出[x.y]
        if (mp[x] * 2 > n * m)
            cout << x;
    }
}

[例4]
在这里插入图片描述
在这里插入图片描述
评测系统

对于每种可能的颜色,我们尝试计算如果将走廊涂成这种颜色需要多少天。在遍历过程中要跳过已经是这种颜色的房子,每次移动k步。最后记录对于这种颜色所需的涂色天数,并与之前的最小天数进行比较,取较小值。
输入数据的最大值即为初始颜色种类数

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        vector<int> a(n);
        int maxnum = 0;//记录初始颜色种类数
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            maxnum=max(maxnum, a[i]);
        }
        int ans = n;
        for (int c = 1; c <= maxnum; ++c) {
            int cnt = 0;
            int idx = 0;
            while (idx < n) {
                if (a[idx] == c) {
                    idx++;
                    continue;
                }
                idx += k;
                cnt++;
            }
            ans = min(cnt, ans);
        }
        cout << ans << endl;
    }
}

2.模拟

[例1] 扫雷
在这里插入图片描述
在这里插入图片描述
评测系统

#include <iostream>
using namespace std;
const int N = 150;
int main()
{
    int n, m;
    cin >> n >> m;
    int a[N][N], ans[N][N];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] == 1) { 
                ans[i][j] = 9; 
                continue; 
            }
            int i2 = i, j2 = j;//遍历四周
            for (i2 = max(0, i - 1); i2 <= min(n - 1, i + 1); i2++) {
                for (j2 = max(0, j - 1); j2 <= min(m - 1, j + 1); j2++) {
                    if (a[i2][j2] == 1) ans[i][j]++;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << ans[i][j]<<" ";
        }
        cout << endl;
    }
    return 0;
}

[例2] 灌溉
在这里插入图片描述
在这里插入图片描述
评测系统

使用类似于BFS的过程,用bool类型的irrigated数组记录当前位置是否已被灌溉,最终统计已被灌溉的数量。

#include <iostream>
#include <queue>
using namespace std;
const int N = 200;
int main()
{
    int n, m, t;
    cin >> n >> m >> t;
    bool irrigated[N][N];
    queue<pair<int, int>> q;
    for (int i = 0; i < t; i++) {
        int r, c;
        cin >> r >> c;
        r--;
        c--;
        irrigated[r][c] = true;
        q.push({ r,c });
    }
    int k;
    cin >> k;
    for (int minute = 0; minute < k; ++minute) {
        int size = q.size();
        for (int i = 0; i < size; ++i) {
            pair<int,int > fr=q.front();//取到水管的位置坐标
            int x = fr.first;
            int y = fr.second;
            q.pop();
            for (int i2 = max(0,x - 1); i2 <= min(n,x + 1); i2++) { //四周灌溉
                for (int j2 = max(0,y - 1); j2 <= min(m,y + 1); j2++) {
                    if (!irrigated[i2][j2]) {
                        irrigated[i2][j2] = true;
                        q.push({i2, j2});
                    }
                }
            }
        }
    }
    int count = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (irrigated[i][j]) {
                count++;
            }
        }
    }
    cout << count;
    return 0;
}

也可使用map

#include <iostream>
#include <map>
using namespace std;
const int N = 200;
int main()
{
    int n, m, t;
    cin >> n >> m >> t;
    bool irrigated[N][N];
    map<int, int> q;
    for (int i = 0; i < t; i++) {
        int r, c;
        cin >> r >> c;
        r--;
        c--;
        irrigated[r][c] = true;
        q[r]=c;
    }
    int k;
    cin >> k;
    for (int minute = 0; minute < k; ++minute) {
        int size = q.size();
        for (const auto& i:q) {
            int x = i.first;
            int y = i.second;
            for (int i2 = max(0,x - 1); i2 <= min(n,x + 1); i2++) {
                for (int j2 = max(0,y - 1); j2 <= min(m,y + 1); j2++) {
                    if (!irrigated[i2][j2]) {
                        irrigated[i2][j2] = true;
                        q[i2]=j2;
                    }
                }
            }
        }
    }
    int count = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (irrigated[i][j]) {
                count++;
            }
        }
    }
    cout << count;
    return 0;
}

[例3] 回文日期

在这里插入图片描述
在这里插入图片描述
评测系统

#include <iostream>
#include <string>
using namespace std;
bool huiwen(string s) {
    if (s[0] == s[7] && s[1] == s[6] && s[2] == s[5] && s[3] == s[4]) {
        return true;
    }
    else
        return false;
}
bool er(string s) {
    if (s[0] == s[2] && s[2] == s[5] && s[5] == s[7] && s[1] == s[3] && s[3] == s[4] && s[4] == s[6]) {
        return true;
    }
    else
        return false;
}
int pdday(int year, int month) {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        return 31;
    else if (month == 2) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return 29;
        }
        else
            return 28;
    }
    else
        return 30;
}
string zifuchuan(int year, int month, int day) {
    string s=to_string(year);
    if (month / 10 == 0) {
        s += '0' + to_string(month);
    }
    else {
        s += to_string(month);
    }
    if (day / 10 == 0) {
        s += '0' + to_string(day);
    }
    else {
        s += to_string(day);
    }
    return s;
}
int main()
{
    int day, month, year;
    int chushi;
    int day1,day2;
    cin >> chushi;
    day1 = chushi % 10;
    chushi = chushi / 10;
    day2= chushi % 10;
    chushi = chushi / 10;
    day=day2 * 10 + day1;

    day1 = chushi % 10;
    chushi = chushi / 10;
    day2 = chushi % 10;
    chushi = chushi / 10;
    month = day2 * 10 + day1;

    year = chushi;
    bool flag = 0;
    bool pdhuiwen = 0;
    bool pder = 0;
    int i, j, k;
    for (i = year;; i++) {
        for (j = 1; j <= 12; j++) {
            if (flag == 0) {
                j = month;
            }
            for (k = 1; k <= pdday(year,month); k++) {
                if (flag == 0) {
                    k = day+1;
                    flag = 1;
                }
                string s=zifuchuan(i, j, k);
                if (pdhuiwen!=1&&huiwen(s)) {
                    cout << s << endl;
                    pdhuiwen = 1;
                }
                if (pder!=1&&er(s)) {
                    cout << s;
                    pder = 1;
                }
                if (pdhuiwen == 1 && pder == 1) {
                    return 0;
                }
            }
        }
    }
}

[例4] 小蓝和小桥的挑战
在这里插入图片描述
评测系统

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    while (n--) {
        int m;
        cin >> m;
        vector<int> a(m);
        for(int i=0;i<m;i++){
            cin >> a[i];
        }
        int cnt = count(a.begin(), a.end(), 0); // #include <algorithm>
        int sum = 0;
        for (const auto& x : a) {
            sum += x;
        }
        if (sum == 0) {
            cout << cnt + 1;
        }
        else
            cout << cnt<<endl;
    }
}

[例5] DNA序列修正

在这里插入图片描述
在这里插入图片描述

评测系统

#include <iostream>
using namespace std;
bool pipei(char a, char b) {
    return (a == 'A' && b == 'T') || (a == 'T' && b == 'A') || (a == 'C' && b == 'G') || (a == 'G' && b == 'C');
}
int main()
{
    int n;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    int cnt = 0;//计数
    for (int i = 0; i < n; ++i) {
        if (!pipei(s1[i], s2[i])) {
            cnt++;
            for (int j = i + 1; j < n; j++) {
                if (!pipei(s1[j], s2[j])
                    && pipei(s1[i], s2[j])
                    && pipei(s1[j], s2[i])
                    ) {
                    swap(s2[i], s2[j]);
                    break;
                }
            }
        }
    }
    cout << cnt;
}

[例6] 无尽的石头
在这里插入图片描述
在这里插入图片描述
评测系统

#include <iostream>
using namespace std;
int ssum(int x) {
    int sum = 0;
    while (x) {
        sum += x % 10;
        x /= 10;
    }
    return sum;
}
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int x = 0;
        cin >> x;
        int step = 0;
        bool end = 0;
        for (int i = 1; i <= x; i += ssum(i)) {
            if (i == x) {
                cout << step << endl;
                end = 1;
            }
            step++;
        }
        if (end == 0) {
            cout << -1 << endl;
        }
    }
}

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

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

相关文章

科技云报道:从数据到生成式AI,是该重新思考风险的时候了

科技云报道原创。 OpenAI“宫斗”大戏即将尘埃落定。 自首席执行官Sam Altman突然被董事会宣布遭解雇、董事长兼总裁Greg Brockman辞职&#xff1b;紧接着OpenAI员工以辞职威胁董事会要求Altman回归&#xff1b;再到OpenAI董事会更换成员、Altman回归OpenAI。 表面上看&…

数据库容灾的设计与实现(五)

六、容灾方案的应用评估 上文中设计了油田数据级容灾系统&#xff0c;完成了基于Oracle Data Guard数据级容灾架构的设计和实施&#xff0c;实现了Broker Failover的FSFO切换技术、触发器提供不间断服务器端服务、客户端使用TAF实现透明故障转移的&#xff0c;完成了数据级容灾…

Java最全面试题专题---2、Java集合容器(2)

Map接口 说一下 HashMap 的实现原理&#xff1f; HashMap概述&#xff1a; HashMap是基于哈希表的Map接口的非同步实现。此实现提供所有可选的映射操作&#xff0c;并允许使用null值和null键。此类不保证映射的顺序&#xff0c;特别是它不保证该顺序恒久不变。 HashMap的数据…

EasyExcel使用模板导出复杂Excel

1&#xff09;添加easyexlce的依赖 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.0.0-beta1</version> </dependency>2&#xff09;添加excel模板文件 实现的效果&#xff1a;…

java工程(ajax/axios/postman)向请求头中添加消息

1、问题概述 在项目中我们经常会遇到需要向请求头中添加消息的场景&#xff0c;然后后端通过request.getRequest()或者RequestHeader获取请求头中的消息。 下面提供几种前端向请求头添加消息的方式 2、创建一个springmvc工程用于测试 2.1、创建工程并引入相关包信息 sprin…

kettle+report designer导出带样式的excel包含多个sheet页

场景介绍&#xff1a; 运用pentaho report designer报表设计器&#xff0c;查询数据库字典表生成带有样式的excel&#xff0c;通过kettle pentaho报表输出组件导出形成数据字典&#xff0c;最终形成的数据字典样式如下图&#xff1a; 案例适用范围&#xff1a; pentaho repor…

开源治理典型案例分享(汇编转)

当前&#xff0c;越来越多的企业申请通过信通院的开源治理成熟度评估和认证&#xff0c;获得增强级或先进级评估。这些企业包括中国工商银行股份有限公司、中国农业银行、上海浦东发展银行股份有限公司、中信银行股份有限公司、中国太平洋保险&#xff08;集团&#xff09;股份…

停车场物联网解决方案4G工业路由器应用

随着物联网技术的发展&#xff0c;停车场也实现了数字化、智能化。停车场管理系统是一个集计算机、网络通信、自动控制等技术为一体的综合性系统&#xff0c;它的实施&#xff0c;对加强停车场管理&#xff0c;提高工作效率&#xff0c;提升服务质量和现代化水平&#xff0c;进…

ubuntu18.04配置cuda+cudnn+tensorrt+anconda+pytorch-gpu+pycharm

一、显卡驱动安装 执行nvidia-smi查看安装情况 二、cuda安装 cuda官网下载cuda_11.6.2_510.47.03_linux.run&#xff0c;安装执行 sudo sh cuda_11.6.2_510.47.03_linux.run提升安装项&#xff0c;驱动不用安装&#xff0c;即第一项&#xff08;Driver&#xff09;&#xff…

TrustZone之完成器:外围设备和内存

到目前为止,在本指南中,我们集中讨论了处理器,但TrustZone远不止是一组处理器功能。要充分利用TrustZone功能,我们还需要系统其余部分的支持。以下是一个启用了TrustZone的系统示例: 本节探讨了该系统中的关键组件以及它们在TrustZone中的作用。 完成器:外围设备…

centos7部署docker

文章目录 &#xff08;1&#xff09;安装前准备&#xff08;2&#xff09;卸载旧版Docker&#xff08;3&#xff09;安装docker&#xff08;4&#xff09;配置镜像加速 &#xff08;1&#xff09;安装前准备 在开始安装之前&#xff0c;首先需要检查内核版本。使用 uname -r 命…

Python基础期末复习 新手 2

虽然age 10在__init__方法中定义了一个局部变量age&#xff0c;但这个局部变量并不会影响类属性age的值。类属性是在类级别上定义的&#xff0c;不属于任何一个实例。因此&#xff0c;在创建实例s1和s2时&#xff0c;它们的age属性值都为类属性的初始值0。 尽管对类的属性值进…

【JVM从入门到实战】(一) 字节码文件

一、什么是JVM JVM 全称是 Java Virtual Machine&#xff0c;中文译名 Java虚拟机。 JVM 本质上是一个运行在计算机上的程序&#xff0c;他的职责是运行Java字节码文件。 二、JVM的功能 解释和运行 对字节码文件中的指令&#xff0c;实时的解释成机器码&#xff0c;让计算机…

kafka学习笔记--broker工作流程、重要参数

本文内容来自尚硅谷B站公开教学视频&#xff0c;仅做个人总结、学习、复习使用&#xff0c;任何对此文章的引用&#xff0c;应当说明源出处为尚硅谷&#xff0c;不得用于商业用途。 如有侵权、联系速删 视频教程链接&#xff1a;【尚硅谷】Kafka3.x教程&#xff08;从入门到调优…

VSCode中如何查看EDI报文?

VSCode是开发人员常用的一款软件&#xff0c;为了降低EDI报文的阅读门槛&#xff0c;知行的开发人员设计了EDI插件&#xff0c;可以在VSCode中下载使用。 如何打开一个EDI报文——VSCode EDI插件介绍 EDI插件下载流程 进入VSCode&#xff0c;打开Extensions&#xff0c;在搜索…

数组|73. 矩阵置零 48. 旋转图像

73. 矩阵置零 **题目:**给定一个 m x n 的矩阵&#xff0c;如果一个元素为 0 &#xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 题目链接&#xff1a;矩阵置零 class Solution {public void setZeroes(int[][] matrix) {Stack<int[]> mapofzerone…

基于大语言模型的复杂任务认知推理算法CogTree

近日&#xff0c;阿里云人工智能平台PAI与华东师范大学张伟教授团队合作在自然语言处理顶级会议EMNLP2023上发表了基于认知理论所衍生的CogTree认知树生成式语言模型。通过两个系统&#xff1a;直觉系统和反思系统来模仿人类产生认知的过程。直觉系统负责产生原始问题的多个分解…

react中img引入本地图片的方式

在html文件中&#xff0c;可以直接<img src./roadBook.png /> 但是在jsx文件中&#xff0c;不支持这种写法 必须这样写 在css样式中 App.css .img{background: url(./img/roadBook.png) }App.js import ./App.css;<div classNameimg></div> 1.基于es6Mod…

【面试总结】Java面试题目总结(一)

&#xff08;以下仅为个人见解&#xff0c;如果有误&#xff0c;欢迎大家批评并指出错误&#xff0c;谢谢大家&#xff09; 1.项目中的验证码功能是如何实现的&#xff1f; 第一步&#xff1a;在项目的pom.xml文件中导入 EasyCaptcha 的依赖&#xff1b; <dependency>…

联邦蒸馏中的分布式知识一致性 | TIST 2024

联邦蒸馏中的分布式知识一致性 | TIST 2024 联邦学习是一种隐私保护的分布式机器学习范式&#xff0c;服务器可以在不汇集客户端私有数据的前提下联合训练机器学习模型。通信约束和系统异构是联邦学习面临的两大严峻挑战。为同时解决上述两个问题&#xff0c;联邦蒸馏技术被提…
最新文章