常用代码模板

1.快速幂

long long fastpow(long long base, long long power, long long mod) {
	long long sum = 1;
	while (power) {
		if (power % 2) sum = sum * base % mod;//指数为奇数时
		base = base * base % mod;
		power /= 2;
	}
	return sum;
}

2.辗转相除法

int gcd(int a, int b)
{
	if (b == 0)
		return a;
	else
		return gcd(b, a % b);
}

3.二进制枚举

#include<bits/stdc++.h>
using namespace std;
 
 
int main()
{
    int n, x, a[50],ans=0;
    cin >> n >> x;
    for (int i = 0; i < n; i++) cin >> a[i];
     
    for (int i = 0; i<(1 << n); i++) {//表示0~2^n-1个状态
        int sum = 0;
        for (int j = 0; j < n; j++) {//遍历二进制的每一位
            if ((i>>j) & 1) sum += a[j];//判断二进制第j位是否存在
        }
        if (sum == x) ans++;
    }
    cout << ans << endl;
    return 0;
}

4.约瑟夫环

#include<algorithm>
#include<iostream>
using namespace std;
int main(){
	int a[1001]={0}; //初始化化数组作为环
	int n,m;//n代表总的人数,m代表报数到几退出
	cin>>n>>m;
	int count=0;//记录退出的个数
	int k=-1;//这里假定开始为第一个人,下标为0,编号为1,如需从编号x开始,则k=x-2
	while(count<n-1){  //总共需要退出n-1个人
		int i=0;//记录当前报数编号
		while(i<m){
			k=(k+1)%n; //循环处理下标
			if(a[k]==0){
				i++;
				if(i==m){
					a[k]=-1;
					count++;
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		if(a[i]==0){
			printf("%d\n",i+1);
			break;
		}
	}
	return 0;
}

5.dfs-----迷宫问题

#include<bits/stdc++.h>
using namespace std;
int a[10][10], book[10][10], ans;
int row, col, x2, y2;
int next_d[4][2] = { {1,0},{-1,0},{0,1},{0,-1} }; // 四个方向的偏移量
 
void dfs(int x1, int y1);
 
int main()
{
    int n;
    int x1, y1;
    cin >> row >> col >> n; // 输入迷宫的行数、列数和障碍总数
    cin >> x1 >> y1 >> x2 >> y2; // 输入起点坐标和终点坐标
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        book[x][y] = 1; // 标记障碍点
    }
    dfs(x1, y1); // 开始搜索从起点到终点的路径
    cout << ans << endl; // 输出路径总数
    return 0;
}
 
void dfs(int x1, int y1)
{
    if (x1 == x2 && y1 == y2) {
        ans++; // 终点坐标,路径总数加1
        return;
    }
 
    int tx, ty; // 下一个方向的坐标
    for (int i = 0; i < 4; i++) {
        tx = x1 + next_d[i][0]; // 计算下一个方向的x坐标
        ty = y1 + next_d[i][1]; // 计算下一个方向的y坐标
        if (tx < 1 || tx > col || ty < 1 || ty > row) continue; // 越界判断
        if (book[tx][ty] == 0) {
            book[x1][y1] = 1; // 标记当前的点,表示已经访问过
            dfs(tx, ty); // 前往下一个点继续搜索
            book[x1][y1] = 0; // 回溯,将当前点标记为未访问状态
        }
    }
    return;
}

6.bfs----最短路径问题

#include<iostream>
#include<queue>
using namespace std;
 
int mp[500][500]; // 存储迷宫的地图信息
int n, m; // 迷宫的行数和列数
int next_d[4][2] = { {1,0},{-1,0},{0,1},{0,-1} }; // 定义四个方向的偏移量
 
struct pos {
    int x, y, cnt; // 记录坐标和步数
    pos(int a = 0, int b = 0, int c = 0) : x(a), y(b), cnt(c) {} // 初始化结构体
};
 
void bfs(void); // 定义BFS函数
 
int main()
{
    cin >> n >> m; // 输入迷宫的行数和列数
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> mp[i][j]; // 输入迷宫地图信息
        }
    }
    bfs(); // 调用BFS函数求解最短路径
    return 0;
}
 
void bfs(void)
{
    queue<pos> q;
    q.push({ 1,1 }); // 将起点入队
    mp[1][1] = 1; // 标记起点为已访问
 
    while (!q.empty()) { // 当队列不为空时,继续搜索
        struct pos p = q.front(); // 取出队首元素
        q.pop();
 
        if (p.x == n && p.y == m) { // 如果当前坐标是终点,输出步数并返回
            cout << p.cnt << endl;
            return;
        }
 
        for (int i = 0; i < 4; i++) { // 遍历四个方向
            int tx = p.x + next_d[i][0]; // 计算新的横坐标
            int ty = p.y + next_d[i][1]; // 计算新的纵坐标
 
            if (tx < 1 || tx > n || ty < 1 || ty > m) continue; // 如果新坐标越界,跳过
            if (mp[tx][ty] == 0) { // 如果新坐标是可通过的,入队并标记为已访问
                q.push({ tx, ty, p.cnt + 1 });
                mp[tx][ty] = 1;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值