小言_互联网的博客

2021蓝桥杯省赛B组c/c++部分题目

340人阅读  评论(0)

前言

复习一下… 虽然我是A组的,但是总觉得B组一些题目也值得看看。并且A, B组有一些类似.

蓝桥杯刷题汇集

推荐阅读:

蓝桥杯省赛 C/C++ ABC组题解(第四届 ~ 第十二届)

2021年第十二届蓝桥杯省赛B组C/C++题解

2021年第十二届蓝桥杯C++B组省赛个人题解



题目

试题A 空间

#include <iostream>
using namespace std;

int main() {
   
	long long n = 256;
	cout << n * 1024 * 1024 / 4 << endl;  // 67108864
} 

答案:67108864



试题F 时间显示

试题图片来自: https://blog.csdn.net/weixin_46660078/article/details/115919426

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
   
	long long n;
	cin >> n;
	n /= 1000;  // ms -> s
	n %= (3600 * 24); 
	int hour = n / 3600;
	int min = n / 60 % 60;
	int sec = n % 60;
	cout << setw(2) << setfill('0') << setiosflags(ios::right);
	cout << hour << ":";
	cout << setw(2) << setfill('0') << min << ":";
	cout << setw(2) << setfill('0') << sec << endl;
} 

这里我为了方便测试,套上了循环。

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
   
	long long n;
	while(cin >> n) {
   
	n /= 1000;  // ms -> s
	n /= (3600 * 24); 
	int hour = n / 3600;
	int min = n / 60 % 60;
	int sec = n % 60;
	cout << setw(2) << setfill('0') << setiosflags(ios::right);
	cout << hour << ":";
	cout << setw(2) << setfill('0') << min << ":";
	cout << setw(2) << setfill('0') << sec << endl;}
} 



试题H 杨辉三角形

试题图片来自: https://blog.csdn.net/weixin_46660078/article/details/115919426

先贴个别人代码:

#include <iostream>
using namespace std;

int main() {
   
	long long n, res = 1;
	cin >> n;
	for (int i = 1;;i++) {
   
		long long j, cnt = 1, flag = 0;
		if ((i * (i - 1)) / 2 > n) {
   
			res = n * (n + 1) / 2 + 2;
			break;
		}
		if (cnt == n)
			break;
		for (j = 0; j <= (i + 1) / 2; j ++) {
   
			if (cnt > n)
				break;
			else if (cnt == n) {
   
				res += j + 1;
				flag = 1;
				break;
			}
		}
		if (flag)
			break;
		else
			res += i + 1;
	}
	cout << res << endl;
}



试题I 双向排序

试题图片来自: https://blog.csdn.net/qq_35975367/article/details/116277383

暂时只会暴力,由于大后天就比赛了。就不想着正解了…(以后有空再更, 留下不学无术的眼泪qaq)
代码修改自: https://zhuanlan.zhihu.com/p/366002603

#include <iostream> 
#include <algorithm>
using namespace std;

bool B2Scmp(int a, int b) {
   
	return a > b;
}

bool S2Bcmp(int a, int b) {
   
	return a < b;
}

//暴力分 n*n*logn
int main() {
   
	int a[100005];
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		a[i] = i;
	while(m--) {
     // 1e5
		int p, q;
		cin >> p >> q;
		// nlogn
		if (p == 0) {
   
			sort(a + 1, a + q + 1, B2Scmp);
		} else {
   
			sort(a + q, a + n + 1, S2Bcmp);
		}
//		for (int i = 1; i <= n; i++)
//			cout << a[i] << " ";
//		cout << endl;
	}
	for (int i = 1; i <= n; i++) {
   
		cout << a[i];
		if (i != n)
			cout << " ";
		else
			cout << endl;
	}
}



试题J 括号序列

试题图片来自: https://blog.csdn.net/weixin_46660078/article/details/115919426

代码来源(略微小改): https://blog.csdn.net/weixin_46660078/article/details/115919426
暂时看不懂…

#include <iostream>
#include <cstring>
#include <algorithm> 
using namespace std;

const int mod = 1e9+7;
const int N = 5e3+7;
int n;
string s;
long long dp[N][N];

long long  cale(){
   
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for(int i = 1; i <= n; i++)
        if(s[i-1] == '(')
            for(int j = 1;j <= n; j++)
                dp[i][j] = dp[i-1][j-1];
        else {
   
            dp[i][0] = (dp[i-1][0] + dp[i-1][1]) % mod;
            for(int j = 1; j <= n; j++)
                dp[i][j] = (dp[i-1][j+1] + dp[i][j-1]) % mod;
        }
    for(int i = 0;i <= n; i++)
        if(dp[n][i])
            return dp[n][i];
    return -1;
}


int main(){
   
    cin >> s;
    n = s.length();
    long long l = cale();
    reverse(s.begin(), s.end());
    for(int i = 0; i < n; i++)
        if(s[i] == '(')
			s[i] = ')';
        else 
			s[i] = '(';
    long long r = cale();
    cout << l * r % mod;
}

转载:https://blog.csdn.net/weixin_43850253/article/details/117479501
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场