欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

蓝桥杯训练4.18

程序员文章站 2023-12-26 15:40:21
...

1.好好学习

#include<bits/stdc++.h>
using namespace std;
int main(){
	int cnt = 0, ans = 0;
	vector<int>w;
	w.push_back(1);
	w.push_back(1);
	w.push_back(2);
	w.push_back(3);
	do{
		if(w[0] == 1 && w[1] == 1 && w[2] == 2 && w[3] == 3)
		ans++;
		cnt++;
	}while(next_permutation(w.begin(), w.end()));
	int temp = __gcd(ans, cnt);
	cout << ans / temp << "/" << cnt / temp << endl;
	return 0;
}

答案:1/12

2.埃及分数

#include<bits/stdc++.h>
using namespace std;
int main(){
	int ans = 0;
	for(int i = 1; i < 5000; i++){
		for(int j = i + 1; j < 5000; j++){
			int t1 = i * j;
			int t2 = i + j;
			int temp = __gcd(t1, t2);
			t1 /= temp;
			t2 /= temp;
			if(t2 == 2 && t1 == 45)
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

答案:7

注意a,b是不同的整数

3.金蝉素数

答案:k == 5

4.横向打印二叉树

比较恶心的模拟,先建树, 在打印图形。

#include<bits/stdc++.h>
using namespace std;
int n, le[200], ri[200], a[200];
void add(int x, int &node){
	if(node == -1){
		node = x;
		le[x] = ri[x] = -1;
		return ;
	}
	if(a[x] > a[node])
		add(x, ri[node]);
	else
		add(x, le[node]);
	return ;
}
char ans[1000][1000], row;
int fun1(char *ch, int x){
	int len = 0;
	while(x){
		ch[len++] = x % 10 + '0';
		x /= 10;
	}
	ch[len] = 0;
	reverse(ch, ch + len);
	return len;
}
int prin(int x, int &y, int node){
	char num[10];
	int len = fun1(num, a[node]), index_y = y, temp = y;
	if(ri[node] != -1){
		index_y = prin(x + len + 3, y, ri[node]);
		for(int i = index_y + 1; i >= y; i--)
			ans[i][x + len + 1] = '|';
		ans[y][x + len + 2] = '-';
		ans[index_y + 1][x + len] = '-';
		y = index_y + 1;
		temp = y;
	}
	for(int i = 0; i < len; i++){
		ans[temp][x + i] = num[i];
	}
	if(le[node] != -1){
		y++;
		index_y = prin(x + len + 3, y, le[node]);
		for(int i = temp; i <= y; i++)
			ans[i][x + len + 1] = '|';
		ans[y][x + len + 2] = '-';
		ans[temp][x + len] = '-';
	}
	y = temp;
	return max(temp, index_y);
}
void solve(){
	for(int i = 0; i <= row; i++){
		int len = 0;
		for(int j = 0; j < 1000; j++){
			if(ans[i][j])
			len = j;
		}
		for(int j = 0; j <= len; j++){
			if(ans[i][j])
			printf("%c", ans[i][j]);
			else
			printf(".");
		}
		printf("\n");
	}
	return ;
}
int main(){
	n = 1;
	int head = -1;
	while(cin >> a[n]){
		add(n++, head);
	}
//	for(int i = 1; i <= n; i++)
//	cout << le[i] << " " << ri[i] << endl;
	int yy = 0;
	row = prin(0, yy, 1);
	solve();
	return 0;
}



5.危险系数

O(n)暴力,并查集判断,总复杂度O(n * (n + m))

6.公式求值

比较难,参考这篇题解

https://www.cnblogs.com/gangduo-shangjinlieren/p/4372897.html





上一篇:

下一篇: