枚举和模拟算法笔记

枚举:

不断猜测,从可能的答案集合中一一尝试,然后再判断题目的条件是否成立。枚举每一种可能性

1.找对答案集合 1~~100最大的质数

2.答案成立的条件--->题目给出

3.选择合适的枚举顺序

4.减小枚举范围---->提高效率

大多数用for循环就行,但有些不行

搜索 dfs bfs ----暴力搜索

eg.全排列 1~3 123 132 213 231 312 321中哪个符合....条件

模拟

直接按照要求操作,思路简单,但操作和代码量大

有的题目不会直接告诉你操作步骤,需要自己去分析

模拟和枚举统称暴力

做题步骤

1.读题面,看样例

2.看数据范围,确定时间复杂度和数据类型

例题1:https://www.luogu.com.cn/problem/P1003

cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int n;
    cin>>n;
    //定义一个二维数组 n行4列
    vector<vector<int>> vec(n,vector <int>(4,0));
    int a,b,g,k;
    for(int i=0;i<n;i++){
            scanf("%d %d %d %d",&a,&b,&g,&k);
            vec[i][0]=a;vec[i][1]=b;vec[i][2]=g;vec[i][3]=k;
        }
    int x,y;
    scanf("%d %d",&x,&y);
    for(int i=n-1;i>=0;i--){
        if(x<=vec[i][0]+vec[i][2]&&x>=vec[i][0]&&y>=vec[i][1]&&y<=vec[i][1]+vec[i][3]){
             printf("%d",i+1);
           return 0;
        }
    }
    printf("%d",-1);
}

例题2:59. 螺旋矩阵 II - 力扣(LeetCode)

cpp 复制代码
class Solution {
public:
    
     vector<vector<int>> generateMatrix(int n) {
     vector<vector<int>> arr(n, vector<int>(n, 0));
     int i = 1;
     int a = 0, b = n - 1, c = 0, d = n - 1;
     while (i <= n * n)
     {
         for (int x = a; x <= b; x++)
         {
             arr[c][x] = i;
             i++;
         }
        
         c++;
         for (int x = c; x <= d; x++)
         {
             arr[x][b] = i;
             i++;
         }
         b--;
         for (int x = b; x >= a; x--)
         {
             arr[d][x] = i;
             i++;
         }
         d--;
         for (int x = d; x >= c; x--)
         {
             arr[x][a] = i;
             i++;
         }
         a++;
     }
     return arr;
 }
};

例3.https://www.luogu.com.cn/problem/P1328#ide

cpp 复制代码
#include<iostream>
using namespace std;
int main()
{
    int N,NA,NB;
    int sum1=0;
    int sum2=0;
    cin>>N>>NA>>NB;
    //二维数组存储表格,下标正好表示出的手势
    int arr[5][5]{0,0,1,1,0,
                  1,0,0,1,0,
                  0,1,0,0,1,
                  0,0,1,0,1,
                  1,1,0,0,0};//这是甲对乙的结果,那么乙对甲呢?下标调换一下就行了
    //周期性                 //其实应该理解为前对后的结果
    int A[200],B[200];
    for(int i=0;i<NA;i++){
        cin>>A[i];
    }
    for(int i=0;i<NB;i++){
        cin>>B[i];
    }
    int temp1=0,temp2=0;
    for(int i=0;i<N;i++){
      temp1=temp2=i;
      temp1%=NA;  //周期的下标
      temp2%=NB;
    
    sum1+=arr[A[temp1]][B[temp2]]; //赢了就得分,否则不得分
    sum2+=arr[B[temp2]][A[temp1]];
    }
    printf("%d %d",sum1,sum2);
}
相关推荐
天疆说1 小时前
策略的进化:从随心所欲到稳健前行
算法
冻柠檬飞冰走茶1 小时前
《数据结构实验指导-C++语言版》 在顺序表 list 中查找元素 x
开发语言·数据结构·c++·算法·list
Brilliantwxx1 小时前
【C++】 高阶数据结构图(1)并查集
开发语言·数据结构·c++
小鱼仙官2 小时前
Ubuntu C++ NTP校时程序
linux·c++·ubuntu
happyprince2 小时前
03-深刻观-CodeX哲学与升华(源码)
算法·ai编程
不会打球的王子2 小时前
Day 27:迁移学习与微调 — 站在巨人的肩膀上
算法
老当益壮梁奶奶2 小时前
Linux软件编程学习笔记(二)Linux文件IO编程入门:从fopen到fgetc
linux·c语言·c++·笔记·学习
sunoo-2292 小时前
C 语言文件 IO 全攻略:从基础函数到实战踩坑(BMP 读取 + 词典查询)
linux·c语言·前端·笔记·vscode·学习
雪的季节2 小时前
C++ 高级(泛型编程与模板)(有空再研究吧)
c++