藍橋杯 第十一屆軟件類第二次校內模擬賽——C/C++程序設計

第一題

思路:


簡單排列組合問題;
答案7!2!\frac{7!}{2!}

第二題

思路:


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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章