2020 11-14 蓝桥杯C++ B组国赛题目合集
国赛过去一段时间了,发挥很烂,看了蛮多的题解的,来记录一下这些题。
填空题
扩散(搜索)
很标准的深搜,比赛的时候不知道为什么写了一个一直卡住的
#include<bits/stdc++.h>
#include<queue>
using namespace std;
int n, change[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
int vis[10000][10000];
long long cnt;
struct node{
int x, y, tm;
}s, t;
void dfs(){
queue<node> q;
q.push((node){2020 + 2100, 11 + 2100, 0});
q.push((node){11 + 2100, 14 + 2100, 0});
q.push((node){2000 + 2100, 2000 + 2100, 0});
q.push((node){0 + 2100, 0 + 2100, 0});
vis[2020 + 2100][11 + 2100] = vis[11 + 2100][14 + 2100] = vis[2000 + 2100][2000 + 2100] = vis[0 + 2100][0 + 2100] = 1;
cnt = 4;
while(q.size()){
t = q.front();
q.pop();
s.tm = t.tm + 1;
for(int i = 0; i < 4; i++){
s.x = t.x + change[i][0];
s.y = t.y + change[i][1];
if(vis[s.x][s.y] == 0 && s.tm <= n){
vis[s.x][s.y] = 1;
cnt++;
q.push(s);
}
}
}
cout << cnt;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
dfs();
return 0;
}
阶乘约数( 数学)
把2-100的每一个数字进行分解,记录下每一个素因子的个数,每个素因子出现的次数再+1就是它可以选择的种数,把所有的乘起来就是答案了
#include<bits/stdc++.h>
using namespace std;
int a[105];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int k, x, y;
for(int i = 0; i <= 100; i++){
x = i;
for(int j = 2; j * j <= i; j++){
while(x % j == 0){
a[j]++;
x = x / j;
}
}
if(x != 1) a[x]++;
}
long long ans = 1;
for(int i = 1; i <= 100; i++){
if(a[i]) ans = ans * (a[i] + 1);
}
cout << ans;
return 0;
}
本质上升序列(字符串处理)
对字符串进行处理,把得到的子序列存到队列里面,然后用map来记录,用一个指针来存下子序列的位置
#include<bits/stdc++.h>
using namespace std;
char s[500] = "tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl";
queue<pair<string, int> > q;
map<string, int> vis;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
long long ans;
for(int i = 0; i < 200; i++){
string str;
str = str + s[i];
if(vis[str] == 0){
vis[str] = 1;
q.push(make_pair(str, i));
ans++;
}
}
while(q.size()){
string str = q.front().first;
int pos = q.front().second;
q.pop();
for(int i = pos + 1; i < 200; i++){
if(s[i] > s[pos] && vis[str + s[i]] == 0){
vis[str + s[i]] = 1;
q.push(make_pair(str + s[i], i));
ans++;
}
}
}
cout << ans;
return 0;
}
蛇(搜索)
简单的DFS,搜索四个点,然后没深一层sum++,sum=总面积时,次数+1
#include<bits/stdc++.h>
using namespace std;
int ans, v[5][5], yd[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void dfs(int x, int y, int sum){
if(sum == 16){
ans++;
return;
}
for(int i = 0; i < 4; i++){
int fx = x + yd[i][0];
int fy = y + yd[i][1];
if(fx < 1 || fy < 1 || fy > 4 || fx > 4 || v[fx][fy] == 1) continue;
v[fx][fy] = 1;
dfs(fx, fy, sum + 1);
v[fx][fy] = 0;
}
}
int main(){
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
memset(v, 0, sizeof(v));
v[i][j] = 1;
dfs(i, j, 1);
}
}
cout << ans;
return 0;
}