2020蓝桥杯省赛C++B组

1508: [蓝桥杯2020初赛] 门牌制作
#include <bits/stdc++.h>  
using namespace std;  
int main() {  
	int n, ans = 0; n = 1;  
	while(n ++) {  
		if(n == 2021) break;  
		int temp = n;  
		while(temp != 0) {  
			int t = temp % 10;  
			temp /= 10;  
			if(t == 2) ans += 1;  
		}  
	}  
	cout << ans << endl;  
	return 0;  
}
#include <bits/stdc++.h>  
using namespace std;  
int main() {  
	cout << 624 << endl;  
	return 0;  
}
1509: [蓝桥杯2020初赛] 既约分数
#include <bits/stdc++.h>  
using namespace std;  
set<pair<int, int> > s;  
int gcd(int x, int y) {  
	return y ? gcd(y, x%y) : x;  
}  
int main() {  
	for(int i = 1; i <= 2020; i ++) {  
		for(int j = i+1; j <= 2020; j ++) {  
			if(gcd(i, j) == 1) s.insert({i, j});  
		}  
	}  
	cout << s.size() * 2 + 1 << endl;  
	return 0;  
}
#include <bits/stdc++.h>  
using namespace std;  
int main() {  
	cout << 1240607 * 2 + 1 << endl;  
	return 0;  
}
1510: [蓝桥杯2020初赛] 蛇形填数
#include <bits/stdc++.h>  
using namespace std;  
int main() {  
	int step = 4, n = 1, res = 1;  
	while(res ++) {  
		if(res == 21) break;  
		n += step; step += 4;  
	}  
	cout << n << endl;  
	return 0;  
}
#include <bits/stdc++.h>  
using namespace std;  
int main() {  
	cout << 761 << endl;  
	return 0;  
}
1511: [蓝桥杯2020初赛] 七段码
#include <bits/stdc++.h>  
using namespace std;  
const int N = 10;  
int e[N][N];  
bool st[N];  
int p[N];  
int find(int x) {  
	if(p[x] != x) p[x] = find(p[x]);  
	return p[x];  
}  
int main() {  
	e[1][2] = e[1][6] = 1;  
	e[2][1] = e[2][7] = e[2][3] = 1;  
	e[3][2] = e[3][7] = e[3][4] = 1;  
	e[4][3] = e[4][5] = 1;  
	e[5][4] = e[5][7] = e[5][6] = 1;  
	e[6][1] = e[6][7] = e[6][5] = 1;  
	e[7][2] = e[7][3] = e[7][5] = e[7][6] = 1;  
	int ans = 0;  
	for(int i = 1; i < 1<<7; i ++) {  
		memset(st, 0, sizeof st);  
		for(int j = 1; j <= 7 ; j ++) p[j] = j;  
		for(int j = 0; j < 7; j ++) {  
			if((i>>j) & 1) st[j+1] = true;  
		}  
		for(int k = 1; k <= 7 ; k ++) {  
			for(int j = 1; j <= 7; j ++) {  
				if(e[k][j] && st[k] && st[j]) {  
					int pk = find(k), pj = find(j);  
					if(pk != pj) p[pk] = pj;  
				}  
			}  
		}  
		int cnt = 0;  
		for(int j = 1; j <= 7; j ++) {  
			if(st[j] && p[j] == j) cnt ++;  
		}  
		if(cnt == 1) ans ++;  
	}  
	cout << ans << endl;  
	return 0;  
}
#include <bits/stdc++.h>  
using namespace std;  
int main() {  
	cout << 80 << endl;  
	return 0;  
}
1513: [蓝桥杯2020初赛] 跑步锻炼
#include <bits/stdc++.h>  
using namespace std;  
int m[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  
bool check(int y) {  
	if(y%400==0 || ((y%4 == 0)&&(y%100!=0))) return true;  
	else return false;  
}  
int main () {  
	int year = 2000, month, day, res = 6, ans = 0;  
	for( ; year <= 2019; year ++) {  
		month = 1; int flag = 0;  
		if(check(year)) flag = 1; m[2] += flag;  
		for( ; month <= 12; month ++) {  
			day = 1;  
			for( ; day <= m[month]; day ++) {  
				if(day == 1 || res == 1) ans += 2;  
				else ans += 1;  
				res = (res+1) % 7;  
			}  
		}  
		m[2] -= flag;  
	}  
	m[2] += 1; month = 1;  
	for( ; month <= 9; month ++) {  
		day = 1;  
		for( ; day <= m[month]; day ++) {  
			if(day == 1 || res == 1) ans += 2;  
			else ans += 1;  
			res = (res+1) % 7;  
		}  
	}  
	cout << ans + 2 << endl;  
	return 0;  
}
#include <bits/stdc++.h>  
using namespace std;  
int main() {  
	cout << 8879 << endl;  
	return 0;  
}
1518: [蓝桥杯2020初赛] 回文日期
#include <bits/stdc++.h>  
using namespace std;  
bool cmp(string a, string b) {  
	return a < b;  
}  
int main() {  
	int T; cin >> T;  
	while(T --) {  
		string date;  
		int i, j, y;  
		char buff[9];  
		vector<string> str;  
		for(i = 1; i <= 31; i ++) {  
			for(j = 1; j <= 12; j ++) {  
				if (i > 29 && j == 2) continue;  
				else if (i == 31 && (j == 4 || j == 6 || j == 9 || j == 11)) continue;  
				y = (i % 10) * 1000 + (i / 10) * 100 + (j % 10) * 10 + j / 10;  
				if (i == 29 && j == 2 && !((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) continue;  
				sprintf(buff, "%04d%02d%02d", y, j, i);  
				str.push_back(buff);  
			}  
		}  
		sort(str.begin(), str.end(), cmp);  
		cin >> date;  
		for(i = 0; i < str.size(); i ++) {  
			if (date < str[i]) {  
				cout << str[i] << "\n";  
				for(j = i; j < str.size(); j ++) {  
					if (str[j][0] == str[j][2] && str[j][1] == str[j][3])  
					{  
						cout << str[j] << "\n";  
						break;  
					}  
				}  
				break;  
			}  
		}  
	}  
	return 0;  
}
1522: [蓝桥杯2020初赛] 成绩统计
#include <bits/stdc++.h>  
using namespace std;  
int main() {  
	int n; cin >> n;  
	double HG, YX;  
	for(int i = 1; i <= n; i ++) {  
		int sorce; cin >> sorce;  
		if(sorce >= 60) HG = HG + 1.0;  
		if(sorce >= 85) YX = YX + 1.0;  
	}  
	double AHG = HG / (1.0 * n);  
	double AYX = YX / (1.0 * n);  
	cout << (int)(AHG * 100 + 0.5) << '%' << endl;  
	cout << (int)(AYX * 100 + 0.5) << '%' << endl;  
	return 0;  
}
1523: [蓝桥杯2020初赛] 子串分值和

为了简便我们直接拿:字符串S(ababc)来说:
以s[1]结尾的字符串个数:
a
以s[2]结尾的字符串个数:
b
ab
以s[3]结尾的字符串个数:
a
ba
aba
以s[4]结尾的字符串个数:
b
ab
bab
abab
以s[5]结尾的字符串个数:
c
bc
abc
babc
ababc
对此分析可以发现这就是所有的子字符串,并且对于以 S[i] 结尾的个数 dp[i] 和 S[i-1] 结尾的个数 dp[i-1] 肯定有关系,即 dp[i] = dp[i-1] + i,所以有 dp[i] = dp[i-1] + i - indx[M],indx[M] 表示当前字符在上一次出现的位置,这样的话可以确定出当前范围的子串分值为多少。从而解决本道题目。

#include <bits/stdc++.h>  
using namespace std;  
int dp[100010];  
int indx[50];  
int main() {  
	char s[100010]; cin >> s+1;  
	long long ans = 0;  
	int len = strlen(s+1);  
	for(int i = 1; i <= len; i ++) {  
		dp[i] = dp[i-1] + i - indx[int(s[i]) - int('a')];  
		ans += dp[i];  
		indx[int(s[i]) - int('a')] = i;  
	}  
	cout << ans << endl;  
}
1524: [蓝桥杯2020初赛] 平面切分

分析题目之前需要知道一个数学相关的只是,即在同一个平面内,如果添加一条直线,与平面所有的直线不相交,则会增加一个平面,如果与这个平面内的一条直线相交并且产生不同的位置的交点,那么就会额外增加一个平面。
那么我们在做题的过程中,只需判断是否时重边,计算新增加的直线与前面的直线有多少个不同的交点,重边非常的好判断,直接用一个数组标记一下即可,计算交点我们也可以用C++的stl库内的set(可以自动去重),从而可以解决本题。

#include <bits/stdc++.h>  
using namespace std;  
const int N = 1010;  
typedef pair<double, double> PDD;  
bool st[N];  
double a[N][2];  
int main() {  
	int n, ans = 1; cin >> n;  
	for(int i = 1; i <= n; i ++) {  
		cin >> a[i][0] >> a[i][1];  
		set<PDD> point;  
		for(int j = 1; j <= i - 1; j ++) {  
			if(st[j]) continue;  
			if(a[i][0] == a[j][0]) {  
				if(a[i][1] == a[j][1]) {  
					st[i] = true;  
					break;  
				}  
				else continue;  
			}  
			double x = (a[i][1] - a[j][1]) / (a[j][0] - a[i][0]);  
			double y = a[i][0] * x + a[i][1];  
			point.insert({x, y});  
		}  
		if(!st[i]) ans += point.size() + 1;  
	}  
	cout << ans << endl;  
	return 0;  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值