蓝桥杯 第十一届软件类第二次校内模拟赛——C/C++程序设计

第一题

思路:


简单排列组合问题;
答案 7 ! 2 ! \frac{7!}{2!} 2!7!

第二题

思路:


dfs搜一下就行了

代码:

#include<bits/stdc++.h>
#define crr(x) cerr << "#x:" << ' ' << x << '\n';

using namespace std;

int n;

int dfs(int p) {
    
    	
	if(p == 8) {
    
    
		return n == 0;
	}
	int ans = 0;
	++n;
	ans += dfs(p + 1);
	--n;
	if(n > 0) {
    
    
		--n;
		ans += dfs(p + 1);
		++n;	
	}
	return ans;
}

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	cout << dfs(0);
	return 0;
}

第三题

思路:


1MB=1024KB=1024*1024Byte

第四题

思路:


2018

第五题

思路:


所有数检查一下即可;

代码:

#include<bits/stdc++.h>
#define crr(x) cerr << "#x:" << ' ' << x << '\n';

using namespace std;

int n, a, b, c;

bool ok(int x) {
    
    
	if(x % a == 0) return false;
	if(x % b == 0) return false;
	return x % c;
}

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	cin >> n >> a >> b >> c;
	int ans = 0;
	for(int i = 1; i <= n; i++) {
    
    
		if(ok(i)) ++ans;	
	}
	cout << ans;
	return 0;
}

第六题

思路:


按题意操作即可;

代码:

#include<bits/stdc++.h>
#define crr(x) cerr << "#x:" << ' ' << x << '\n';

using namespace std;

void add(char & c) {
    
    
	if(c >= 'a' && c <= 'w') c = c + 3;
	else c = c - 23;	
}

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	string s;
	cin >> s;
	for(int i = 0; i < s.length(); i++) {
    
    
		add(s[i]);	
	}
	cout << s;
	return 0;
}

第七题

思路:


先计算该点在第几圈,然后再判断它在哪条边即可;

代码:

#include<bits/stdc++.h>
#define crr(x) cerr << "#x:" << ' ' << x << '\n';

using namespace std;

void run(int n, int m, int r, int c) {
    
    
	int k = min(r, c);
	k = min(k, n - r + 1);
	k = min(k, m - c + 1);
	int ans = 0;
	for(int i = 1; i < k; i++) {
    
    
		ans += 2 * (m + n) - 4;
		m -= 2; n -= 2; --r; --c;
	}
	if(r == 1) {
    
    
		cout << ans + c;
		return;	
	}
	ans += m - 1;
	if(c == m) {
    
    
		cout << ans + r;
		return;	
	}
	ans += n - 1;
	if(r == n) {
    
    
		cout << ans + m - c + 1;
		return;	
	}
	ans += m - 1;
	cout << ans + n - r + 1;
}

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	int n, m, r, c;
	cin >> n >> m >> r >> c;
	run(n, m, r, c);
	return 0;
}

第八题

思路:


用dp[i][j]记录第i项数字为j的种数;
递推关系就显而易见了;

代码:

#include<bits/stdc++.h>
#define crr(x) cerr << "#x:" << ' ' << x << '\n';

using namespace std;

const int mod = 10000;

int n, m, dp[1005][1005];

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	cin >> m >> n;
	for(int i = 1; i <= n; i++) dp[1][i] = 1;
	for(int i = 2; i <= m; i++) {
    
    
		if(i & 1) {
    
    
			for(int j = 2; j <= n; j++) {
    
    
				dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod;	
			}
		}
		else {
    
    
			for(int j = n - 1; j >= 1; j--) {
    
    
				dp[i][j] = (dp[i][j + 1] + dp[i - 1][j + 1]) % mod;	
			}
		}
	}
	int ans = 0;
	for(int i = 1; i <= n; i++) ans += dp[m][i];
	cout << ans % mod;
	return 0;
}

第九题

思路:


用dfs搜索一下;
提前计算一下后缀和数组,可以方便剪枝;

代码:

#include<bits/stdc++.h>
#define crr(x) cerr << "#x:" << ' ' << x << '\n';
#define f(x) ((x) * (x))

using namespace std;

int n, x[1005], y[1005], r[1005];
int sum[1005];
bool flag[1005];
int ans;

bool ok(int & p) {
    
    
	for(int i = 0; i < n; i++) {
    
    
		if(i == p || flag[i] == false) continue;
		int dis2 = f(x[i] - x[p]) + f(y[i] - y[p]);
		if(dis2 < f(r[p] + r[i])) return false;
	}
	return true;
}
	
void dfs(int p, int rcd) {
    
    
	if(sum[p] + rcd <= ans) return;
	if(p == n) {
    
    
		ans = max(ans, rcd);
		return;	
	}
	if(ok(p)) {
    
     
		flag[p] = true;
		dfs(p + 1, rcd + r[p] * r[p]);
	}	
	flag[p] = false;
	dfs(p + 1, rcd);	
}

int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	cin >> n;
	for(int i = 0; i < n; i++) {
    
    
		cin >> x[i] >> y[i] >> r[i];	
	}
	sum[n - 1] = f(r[n - 1]);
	for(int i = n - 2; i >= 0; i--) {
    
    
		sum[i] = sum[i + 1] + f(r[i]);	
	}
	dfs(0, 0);
	cout << ans;
	return 0;
}

第十题

思路:


先在O(n^n)内计算出所有的边权;
然后用kruskal算法求出最小生成树即可;
(prim不用堆优化的话会超时)

代码:

#include<bits/stdc++.h>
#define crr(x) cerr << "#data:" << ' ' << x << '\n';

using namespace std;

int par[1005], rk[1005];

void init_set(int n) {
    
    
	for(int i = 1; i <= n; i++) par[i] = i;
}

int find(int x) {
    
    
	if(x == par[x]) return x;
	return par[x] = find(par[x]);	
}

void unite(int x, int y) {
    
    
	x = find(x);	
	y = find(y);
	if(x == y) return;
	if(rk[y] > rk[x]) par[x] = y;
	else {
    
    
		par[y] = x;
		if(rk[x] == rk[y]) ++rk[x];
	}
}

bool same(int x, int y) {
    
    
	return find(x) == find(y);	
}

struct edge {
    
    
	int u, v;
	double cost;
	bool operator < (const edge & e) {
    
    
		return cost < e.cost;
	}
} es[1000005];
int v, e;

double kruskal() {
    
    
	sort(es, es + e);
	init_set(v);
	double res = 0;
	for(int i = 0; i < e; i++) {
    
    
		edge ts = es[i];
		if(!same(ts.u, ts.v)) {
    
    
			unite(ts.u, ts.v);
			res += ts.cost;
		}
	}
	printf("%.2f", res);
}

double x[1005], y[1005], h[1005];

#define f(x) ((x) * (x))
int main() {
    
    
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	cin >> v;
	for(int i = 1; i <= v; ++i) {
    
    
		cin >> x[i] >> y[i] >> h[i];	
	}
	for(int i = 1; i <= v; i++) {
    
    
		for(int j = i + 1; j <= v; j++) {
    
    
			es[e].u = i;
			es[e].v = j;
			es[e++].cost = sqrt(f(x[i] - x[j]) + f(y[i] - y[j])) + f(h[i] - h[j]);
		}
	}
	kruskal();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/105566182
今日推荐