【河北经贸大学蓝桥杯热身赛】

文章包含一系列编程题的解法,主要涉及计算最大公约数(GCD)、判断素数、蛇形填数矩阵、暴力搜索求解问题以及Dijkstra最短路径算法的应用。代码示例展示了如何用C++解决这些问题。
摘要由CSDN通过智能技术生成

在这里插入图片描述

B 奇怪的数字对

第二题题目有点问题,可以重复取相同的数

#include<iostream>
using namespace std;
int gcd(int a,int b)
{
    if(b==0)
    {
        return a;
    }
    else
        return gcd(b,a%b);
}
int main()
{
    int res=0;
    for(int i=1;i<=2022;i++)
    {
        for(int j=1;j<=2022;j++)
        {
            if(gcd(i,j)==1)
                res++;
        }
    }
    cout<<res<<endl;
}

C 蛇形填数

提供赛时两位同学的做法

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl

using namespace std;

const int N = 1e5 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;

void run()
{
	int i, j;	
	ll sum = 1;
	for (i = 1; i <= 17; i ++ ) sum += 4*i;
	cout <<sum<<endl;
}

signed main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    cout << fixed; cout.precision(18);
    int T = 1;
    //cin >> T;
    
    while(T -- ) run();
    
}
#include <bits/stdc++.h>

using namespace std;
int a[110][110];
void solve()
{
	int x = 1, cnt = 0;
	for(int i = 1; i <= 100; i++)
	{
		cnt++;
		if(cnt % 2 == 0)
		{
			for(int j = 1, k = i; j <= i && k >= 1; j++, k--)
			{
				a[j][k] = x++;	
			}
		}
		else
		{
			for(int j = i, k = 1; j >= 1 && k <= i; j--, k++)
			{
				a[j][k] = x++;	
			}
		}
	}
	cout << a[18][18] << endl;
}

signed main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T = 1;
	//cin >> T;
	while(T--)solve();
	return 0;
}

E 钞票探测

直接暴力即可,注意一不是素数

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl

using namespace std;

const int N = 1e5 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;

int a[N];

void run()
{
	int i, j;
	int ans = 0;	
	for (i = 1; i <= 2022; i ++ ) {
		int flag = 0;
		for (j = 2; j*j <= i; j ++) {
			if(i % j == 0) {
				flag = 1;
				break;
			}
		}
		if(!flag)ans += 1;
	}
	cout << 306 << endl;
}

signed main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    cout << fixed; cout.precision(18);
    int T = 1;
    //cin >> T;
    
    while(T -- ) run();
    
}

F 小贸的蓝桥杯成绩

注意要四舍五入,以下两种写法均可

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl

using namespace std;

const int N = 1e4 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;

int a[N];

void run()
{
	cout << fixed << setprecision(0);
	int n; cin >>n;
	int ans = 0;
	int pass = 0, good = 0;
	for(int i = 1; i <= n; i ++) {
		int x; cin >> x;
		if(x >= 60) pass += 1;
		if(x >= 85) good += 1;
	}
	
	double ans1 = round(pass*100.0/n);
	double ans2 = round(good*100.0/n);
	cout << ans1 << "%\n";
	cout << ans2 << "%";
}

signed main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    cout << fixed; cout.precision(18);
    int T = 1;
    //cin >> T;
    
    while(T -- ) run();
    return 0;
    
}
#include<bits/stdc++.h>

using namespace std;

const int N = 10010;

int n;
int a[N];

int main()
{
    cin >> n;
    
    for(int i = 0; i < n; i++)
        cin >> a[i];
    
    int x = 0,y = 0;
    for(int i = 0; i < n; i++)
    {
        if(a[i] >= 60) x ++;
        if(a[i] >= 85) y ++;
    }
    
    cout << (int)((x * 1.0 / n * 100) + 0.5) << "%" << endl;
    cout << (int)((y * 1.0 / n * 100) + 0.5) << "%" << endl;
} 

H 取金币

简单dp,只需要给出状态转移方程即可,如果不会,可以自行百度学习

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl

using namespace std;

const int N = 1e3 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;

int a[N][N];
int f[N][N];

void run()
{
	int i, j;	
	int n; cin >> n;
	for (i = 1; i <= n; i ++ ) {
		for (j = 1; j <= n; j ++) {
			cin >> a[i][j];
		}
	}
	
	for (i = 1; i <= n; i ++ ) {
		for (j = 1; j <= n; j ++ ) {
			f[i][j] = max(f[i-1][j], f[i][j-1]) + a[i][j];
		}
	}
	
	cout << f[n][n] << endl;
}

signed main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    cout << fixed; cout.precision(18);
    int T = 1;
    //cin >> T;
    
    while(T -- ) run();
    
}

I 砝码称重

给出赛时两位同学的写法

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl

using namespace std;

const int N = 1e5 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;

int w[N];
bool f[1010][N];

void run()
{
	int i, j;	
	int n, m = 0; cin >> n;
	for (i = 1; i <= n; i ++) cin >> w[i], m += w[i];
	
	f[0][0] = 1;
	for (i = 1; i <= n; i ++ ) {
		for(j = 0; j <= m; j ++ ) {
			f[i][j] = f[i-1][j] // 上一次可以成功,这次也能成功 
                    ||f[i-1][abs(j-w[i])] // 加上w[i],注意这里取绝对值
                    ||f[i-1][j+w[i]]; // 减去w[i]
 		}
	}
	int ans = 0;
	for (i = 1; i <= m; i++ ) ans += f[n][i];
	cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    cout << fixed; cout.precision(18);
    int T = 1;
    //cin >> T;
    
    while(T -- ) run();
    
}
#include<bits/stdc++.h>

using namespace std;

const int N = 230 , M = 2e5 + 50 , B = 1e5 + 10;

bool f[N][M];//f(i,j)表示只从前i个数中选,和为j的方案,属性:是否为空 
int sum;
int n;
int a[N]; 
int cnt;

int main()
{
	cin>>n;
	
	//砝码有正负两个值 
	for(int i = 1;i <= 2 * n;i ++)
	{
		cin>>a[i];
		
		i ++;
		
		a[i] = -a[i - 1];
		
		sum += a[i - 1];//正的数之和 
	}
	
	//初始化,0肯定可以凑成 
	for(int i = 0;i <= 2 * n;i ++)
	f[i][B] = true; 
	
	for(int i = 1;i <= 2 * n;i ++)//2 * n个物品 
	   for(int j = -sum;j <= sum;j ++)//可以称量的范围 
	   {
	   	   //可以选 
	       if( j - a[i] >= -sum && j - a[i] <= sum ) f[i][j + B] = f[i][j + n + B] + f[i - 1][j - a[i] + B];
		   
		   //不选
		   f[i][j + B] = f[i][j + B] + f[i - 1][j + B];   	
	   } 
	
	//ans
	for(int j = 1;j <= sum;j ++)
	   if(f[2 * n][j + B] == true)//可以凑出
	      cnt ++;
	 
	 cout<<cnt;
	 
	 return 0;
} 

J 荧光夜跑来啦

dijstar模板题,有很多种写法,如果不会可以学一下

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << '=' << x << ',' << #y << '=' << y <<endl

using namespace std;

const int N = 1e5 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> PII;

int h[N], e[N*2], ne[N*2], w[N], idx;

void add(int a, int b, int c) {
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++ ;
//	cout<<idx-1 << ' ' << a << ' ' << e[idx-1] << ' ' << w[idx-1] <<endl; 
}

int d[N], st[N];
void dij() {
	int i, j;
	memset(d, 0x3f, sizeof d);
	memset(st, 0, sizeof st);
	
	d[1] = 0; 
	priority_queue<PII, vector<PII>, greater<PII>> q;
	q.push({d[1], 1});
	
	while(!q.empty()) {
		auto t = q.top(); q.pop();
		int x = t.second;
		if(st[x]) continue;
		st[x] = 1;
		for (i = h[x]; i != -1; i = ne[i]) {
			int y = e[i];
			if(st[y]) continue;
			if(d[y] > d[x] + w[i]) {
				d[y] = d[x] + w[i];
				q.push({d[y], y});
			}
		}
	}
}

void run()
{
	int i, j;	
	int n, m; cin >>n >>m;
	memset(h, -1, sizeof h);
	while(m -- ) {
		int a, b, c; cin >> a >> b >> c;
//		deb2(a, b);
		add(a, b, c);
//         add(b, a, c);
	}
	
	dij();
	if(d[n] <0x3f3f3f3f)cout << d[n] << endl;
    else cout << "impossible";
}

signed main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    cout << fixed; cout.precision(18);
    int T = 1;
    //cin >> T;
    
    while(T -- ) run();
    
}

/*
3 3
1 2 5
2 3 3
1 3 9
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值