【刷题日记】简单的几道蓝桥杯真题
正则问题
题目
题解
首先要确认题目中各个符号的含义。
在正则表达式中,|为逻辑或符号,表示取左边或右边都可以匹配。为了尽可能使字符串长,这道题中应该选择更长的那一边进行匹配。
AC代码
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
string s;
i64 now = 0;
i64 dfs(){
i64 res = 0;
while(now < s.size()){
switch(s[now]){
case 'x': //遇到x
now++;
res++;
break;
case '(': //遇到左括号,递归获取括号内res
now++;
res += dfs();
now++; //注意跳过右括号
break;
case ')': //遇到右括号,该层结束,是时候返回了!!!
return res;
case '|': //遇到逻辑或,注意要取两边的最大值才行!
now++;
res = max(res, dfs());
break;
}
}
return res;
}
int main(){
cin>>s;
cout<<dfs();
return 0;
}
清理水域
题目
题解
由于数据极小,仅102,哪怕O(N4)应该都能过,直接暴力拿下,不理解为什么是困难题。。。(蓝桥的难度划分太迷惑了)
AC代码
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
i64 n,m,t,r1,r2,c1,c2,ans = 0;
i64 mapp[110][110];
int main(){
scanf("%lld %lld",&n,&m);
scanf("%lld",&t);
while(t--){
scanf("%lld %lld %lld %lld",&r1,&c1,&r2,&c2);
for(i64 i = r1;i<=r2;i++)
for(i64 j = c1;j<=c2;j++)
mapp[i][j] = 1;
}
for(i64 i = 1;i<=n;i++)
for(i64 j = 1;j<=m;j++)
if(!mapp[i][j]) ans++;
printf("%lld",ans);
return 0;
}
埃及分数
题目
题解
嘻嘻,妈妈我会做国赛题啦!!!()
求解代码
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
i64 ans = 0;
int main(){
for(i64 a = 2;a<=10000;a++)
for(i64 b = a+1;b<=10000;b++)
if(45 * (a+b) == 2*a*b)
ans++;
cout<<ans;
return 0;
}
半递增序列
题目
题解
对于奇数n,需要选出n//2个数字放在偶数位,剩余数字放在奇数位,容易发现偶数位和奇数位最终都只有一种排列法(顺序递增),所以对于奇数n,答案为C(n, n//2).同理,对于偶数n,答案为C(n, n/2).
综上所述,答案为C(n, n/2).
用帕斯卡三角直接处理组合数即可。
AC代码
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
i64 psk[1100][1100], n;
const i64 MOD = 1e9+7;
int main(){
scanf("%lld",&n);
psk[1][0] = psk[1][1] = 1;
for(i64 i = 2;i<=n;i++){
psk[i][0] = 1;
for(i64 j = 1;j<=n;j++)
psk[i][j] = (psk[i-1][j-1] + psk[i-1][j]) % MOD;
}
printf("%lld",psk[n][n/2]);
return 0;
}

浙公网安备 33010602011771号