2019年第十届蓝桥杯B组C/C++国赛部分题解
怎么说,这个国赛给我整自闭了。五个填空,五个大题,我就写五个填空加第一个大题吧。后面的一言难尽。
A.
题意: 201 9 2 2019^2 20192 x 2 x^2 x2 y 2 y^2 y2,找出最小的x+y使得前式为等差数列,(2019<x<y);
思路:暴力
答案:7020
代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int x = 2019*2019;
for(int i = 2020;i < 8000;i++){
for(int j = i+1;j < 8000;j++){
int a = i*i;
int b = j*j;
if(a-x == b-a) cout << i+j << endl;
}
}
return 0;
}
B.
题意:将2019拆成两个素数和(应该是两个吧,有人说任意个,忘了),有几种方式,交换两个加数算一种,如2+2017 = 2019,2017+2 = 2019算一种
思路:暴力,我当两个做的
答案:1
代码
#include<bits/stdc++.h>
using namespace std;
int p[2020],prime[2020],pri;
int main() {
for(int i = 2;i <= 2019;i++){
bool flag = 0;
for(int j = 2;j <= (int)sqrt(i);j++){
if(i%j == 0){
flag = 1;
break;
}
}
if(!flag) p[i] = 1,prime[++pri] = i;
}
int sum = 0;
for(int i = 1;i <= pri;i++){
int x = 2019-prime[i];
if(p[x]) sum++;
}
cout << sum / 2 << endl;
return 0;
}
C.
题意:约数有x个的最小整数y,记 S x Sx Sx = y,如 S 1 S1 S1 = 1, S 2 S2 S2 = 2, S 3 S3 S3 = 4,求 S 100 S100 S100
思路:暴力
答案:45360
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int f[maxn];
int yue(int x){
int sum = 0;
for(int i = 1;i <= (int)sqrt(x);i++){
if(x%i == 0) {
sum++;
if(x/i != i) sum++;
}
}
return sum;
}
int main() {
for(int i = 1;i < 100000;i++){
int y = yue(i);
if(f[y]) continue;
f[y] = i;
}
cout << f[100] << endl;
return 0;
}
D.
题意:将一个7*7的方格沿着边剪开后,旋转右边那块拼在左边那块的不规则处,要求拼成的还是一个矩形,求有多少种裁剪方法
思路:沿着副对角线上三角和下三角对称。显而易见,剪得边只能向下或向右,统计到副对角线一共有多少种方法
答案:127
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int dp[8][8];
int main() {
for(int i = 1;i <= 7;i++) dp[i][0] = 1;
int sum = 0;
for(int i = 1;i <= 7;i++){
for(int j = 1;j <= 8-i;j++){
dp[i][j] += dp[i-1][j];
dp[i][j] += dp[i][j-1];
}
sum += dp[i][8-i];
}
cout << sum << endl;
return 0;
}
E.
题意:在一个5*5的方格中,起点是(1,1)沿着线走,计算不超过12步走回(1,1)的方法
思路:深搜,将5×5的方格看成6×6,原来在点上就变成了在格子上
答案:208
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};
int m[7][7],sum;
bool valid(int x,int y){
if(x < 1 || x > 6 || y < 1 || y > 6) return false;
return true;
}
void dfs(int x,int y,int step){
if(step > 12) return ;
if(step != 0 && step <= 12){
if(x == 1 && y == 1){
sum++;
return ;
}
}
for(int i = 0;i < 4;i++){
int next_x = x+dx[i];
int next_y = y+dy[i];
if(!valid(next_x,next_y) || m[next_x][next_y]) continue;
m[next_x][next_y] = 1;
dfs(next_x,next_y,step+1);
m[next_x][next_y] = 0;
}
}
int main() {
dfs(1,1,0);
cout << sum << endl;
return 0;
}
F.
题意:给你两个字符串S和T,|S| ≥ |T|,求最少改变S中多少个字符使得在去掉一些不需要的字符后S = T
数据范围最大是(|S|,|T| ≤ 1000)
思路:最长公共子序列,由于是去掉一些不需要的数,所以当从T中第i位找是否和S中有相同时,得从S的第i位开始,前面不能匹配。同理后面也需要一个界限。局部的最长公共子序列
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+10;
char s[maxn],t[maxn];
int dp[maxn][maxn];
int main() {
int tt;cin >> tt;
while(tt--){
cin >> s+1 >> t+1;
int len_s = strlen(s+1);
int len_t = strlen(t+1);
int ma = 0;
for(int i = 1;i <= len_t;i++){
for(int j = i;j <= len_s-(len_t-i);j++){
if(t[i] == s[j]) dp[i][j] = dp[i-1][j-1]+1;
else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
ma = max(ma,dp[i][j]);
}
}
cout << len_t-ma << endl;
}
return 0;
}
以上题目不保证正确性
G和I,也就是第七和第九题,暴力只能过最水的数据,另外两题没写