2020第十一届11月蓝桥杯大赛软件类B组C/C++省赛题解

本文详细解析了2020年11月蓝桥杯大赛软件类B组的C/C++省赛试题,包括A到H共8道题目。涉及填空题和程序设计题,涵盖门牌制作、既约分数、蛇形填数、跑步锻炼、七段码、成绩统计、回文日期和子串分值和等主题,提供了相应的解题思路和代码答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

试题 A:门牌制作(结果填空)

题意
在这里插入图片描述

做法:直接计算。

代码

#include<bits/stdc++.h>
using namespace std;
int ct2(int i) {
  int cnt = 0;
  while(i) {
    if(i%10 == 2) ++cnt;
    i /= 10;
  }
  return cnt;
}
int main() {
  int sum = 0;
  for(int i = 1; i <= 2020; ++i) {
    sum += ct2(i);
  }
  cout << sum;
  return 0;
}

答案:624


试题 B:既约分数(结果填空)

题意
在这里插入图片描述

做法:直接暴力找。

代码

#include<bits/stdc++.h>
using namespace std;
int main() {
  int cnt = 0;
  for(int i = 1; i <= 2020; ++i) {
    for(int j = 1; j <= 2020; ++j) {
      if(__gcd(i, j) == 1) ++cnt;
    }
  }
  cout << cnt;
  return 0;
}

答案:2481215


试题 C:蛇形填数(结果填空)

题意
在这里插入图片描述

做法:暴力打出来。
代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100;
int tu[N][N];
int main() {
  int x = 0, y = 1, cnt = 0;
  for(int i = 1; i <= 60; ++i) {
    if(i&1) ++x;
    else ++y;
    tu[x][y] = ++cnt;
    for(int j = 0; j < i-1; ++j) {
      if(i & 1) {
        tu[x-1][y+1] = ++cnt;
        --x; ++y;
      } else {
        tu[x+1][y-1] = ++cnt;
        ++x; --y;
      }
    }
  }
  for(int i = 1; i <= 20; ++i) {
    for(int j = 1; j <= 20; ++j) {
      cout << tu[i][j] << " ";
    }
    cout << "\n";
  }
  cout << tu[20][20];
  return 0;
}

答案:761

试题 D:跑步锻炼(结果填空)

题意
在这里插入图片描述
做法:一天一天模拟,细节比较多。

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100;
int mon[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
  int res = 0;
  int y = 1999, m = 12, d = 31, w = 4;
  while(1) {
    ++d; w = (w + 1) % 7;
    if(d > mon[m]) {
      ++m; d = 1;
      if(m > 12) {
        ++y; m = 1;
        if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) mon[2] = 29; //闰年
        else mon[2] = 28;
      }
    }
    if(d == 1 || w == 0) ++res;
    ++res;
    //cout << y << " " << m << " " << d << " " << w << " " << res <<  "\n";
    if(y == 2020 && m == 10 && d == 1) break;
  }
  cout << res;
  return 0;
}

答案:8879

试题 E:七段码(结果填空)

题意
在这里插入图片描述
做法:二进制枚举再判断选到的点是否连通(用dfs判断)。
代码

#include<bits/stdc++.h>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint int
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 10;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7};
int tu[N][N];
void init() {
  tu[0][0] = tu[0][1] = tu[0][2] = tu[0][3] = tu[0][4] = tu[0][5] = tu[0][6] = tu[0][7] = 1;
  tu[1][2] = tu[1][3] = 1;
  tu[2][1] = tu[2][4] = tu[2][5] = 1;
  tu[3][1] = tu[3][4] = tu[3][6] = 1;
  tu[4][2] = tu[4][3] = tu[4][5] = tu[4][6] = 1;
  tu[5][2] = tu[5][4] = tu[5][7] = 1;
  tu[6][3] = tu[6][4] = tu[6][7] = 1;
  tu[7][5] = tu[7][6] = 1;
}
int ans;
int fa[N];
void dfs(int u) {
  fa[u] = 0; 
  for(int i = 1; i <= 7; ++i) {
    if(tu[u][i] && fa[i]) dfs(i);
  }
}
int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); }
void solve (int x) {
  int f = 1;
  for(int i = 0; i < 7; ++i) 
    if((x>>i)&1) fa[i+1] = 1;
  for(int i = 1; i <= 7; ++i) {
    if(fa[i]) {
      dfs(i); break;
    }
  }
  for(int i = 1; i <= 7; ++i) {
    if(fa[i]) f = 0;
    fa[i] = 0;
  }
  ans += f;
}
int main() {
  init();
  for(int i = 1; i < (1 << 7); ++i) solve(i);
  cout << ans;
  return 0;
}

答案:80


试题 F:成绩统计(程序设计)

题意点此进入
统计成绩的及格率和优秀率。

做法:直接贪心即可。

代码

#include<bits/stdc++.h>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint int
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 3e5+10;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int main() {
  int n; scanf("%d", &n);
  double jg = 0, yx = 0, x;
  _rep(1, n, i) {
    scanf("%lf", &x);
    if(x >= 85) ++yx;
    if(x >= 60) ++jg;
  }
  printf("%.0lf%\n%.0lf%\n", jg*100/n, yx*100/n);
  return 0;
}

试题 G:回文日期(结果填空)

题意点此进入
计算出下一个回文型日期和ABABBABA日期。

做法:直接暴力判断就好了,需要注意的点是A!=B。

代码

#include<bits/stdc++.h>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint int
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 3e5+10;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int mon[20] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool ishw(string s) {
  int l = 0, r = s.size()-1;
  while(l < r) {
    if(s[l] != s[r]) return false;
    ++l; --r;
  }
  return true;
}
bool isAB(string s) {
  if(s[0] == s[2] && s[0] == s[5] && s[5] == s[7]) 
    if(s[1] == s[3] && s[3] == s[4] && s[4] == s[6])
      if(s[0] != s[1]) return true;
  return false;
}
int main() {
  int n; scanf("%d", &n);
  int y = n / 10000, m = (n / 100) % 100, d = n % 100;
  if((y % 4 == 0 && y % 100) || y % 400 == 0) mon[2] = 29;
  string s, hw, AB;
  int fhw = 1, fAB = 1;
  while(fhw || fAB) {
    ++d;
    if(d > mon[m]) {
      ++m; d = 1;
      if(m > 12) {
        ++y; m = 1;
        if((y % 4 == 0 && y % 100) || y % 400 == 0) mon[2] = 29;
      }
    }
    s = (char)((y/1000%10)+'0');
	s += (char)((y/100%10)+'0');
	s += char(y/10%10+'0');
	s += char(y%10+'0');
    s += char(m/10%10+'0');
	s += char(m%10+'0');
	s += char(d/10%10+'0');
	s += char(d%10+'0');
    if(fhw && ishw(s)) fhw = 0, hw = s;
    if(fAB && isAB(s)) fAB = 0, AB = s;
  }
  cout << hw << "\n" << AB << "\n";
  return 0;
}

答案


试题 H:子串分值和(程序设计)

题意点此进入
在这里插入图片描述

做法:50分的数据方法挺多的,记录字母前缀和再枚举等等。而100分的做法其实是最短小的(又是考场做不出来系列),正确做法复杂度是O(n),统计每个字母的贡献,即这个字母出现在多少个区间中,权值和就是答案。而“每个字母出现在多少个区间中”用如下公式: ( i − p r e [ s [ i ] − ′ a ′ ] ) ∗ ( l e n − i + 1 ) (i-pre[s[i]-'a']) * (len-i+1) (ipre[s[i]a])(leni+1),其中pre[s[i]-‘a’]代表的是上一个相同字母出现的位置, 而这个公式计算的是,包含i左端点的数量*包含i右端点的数量,要减去上一个相同字母出现的位置是防止重复计算(想不出来系列)。

代码

#include<bits/stdc++.h>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint int
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const int INF = 0x3f3f3f3f;
const int N = 3e5+10;
const LL Mod = 1e9+7;
const int M = 1e6+10;
int pre[30];
char s[N];
int main() {
  scanf("%s", s+1);
  int len = strlen(s+1);
  LL res = 0;
  _rep(1, len, i) {
    res += 1ll * (i-pre[s[i]-'a']) * (len-i+1);
    pre[s[i]-'a'] = i;
  }
  cout << res;
  return 0;
}
目前尚未有2024年第十四届蓝桥杯大赛软件C/C++大学B的真题与题解发布,因为该事的时间线可能还未到达公布阶段[^1]。然而,可以基于以往的比形式和内容推测其考察的知识点范围以及提供一些常见的练习方向。 以下是关于如何准备此的一些指导: ### 准备指南 #### 一、熟悉基础算法 掌握基本的数据结构和经典算法对于参者至关重要。这包括但不限于数、链表、栈、队列等数据结构的应用;排序(快速排序、归并排序)、查找(二分法)、动态规划等问题解决方法的学习与实践。 ```cpp // 快速排序实现 (C++) void quickSort(int arr[], int low, int high){ if(low < high){ int pi = partition(arr,low,high); quickSort(arr, low, pi-1); quickSort(arr, pi+1, high); } } int partition (int arr[], int low, int high){ int pivot = arr[high]; int i = (low - 1); for (int j = low; j <= high- 1; j++){ if (arr[j] < pivot){ i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[high]); return (i + 1); } ``` ```java // 快速排序实现 (Java) public static void quickSort(int[] array, int start, int end) { if(start >= end) return; int pivotIndex = partition(array, start, end); quickSort(array, start, pivotIndex - 1); quickSort(array, pivotIndex + 1, end); } private static int partition(int[] array, int start, int end) { int pivotValue = array[end]; int index = start; for(int i=start;i<end;i++) { if(array[i]<pivotValue) { swap(array,i,index++); } } swap(array,end,index); return index; } ``` #### 二、深入理解编程语言特性 无论是使用C++还是Java参加竞,都需要深入了解所选语言的特点及其标准库的功能。例如,在C++中熟练运用STL容器如vector、map等能够极大提高编码效率;而在Java里,则需熟知Collections框架下的各集合型及其实现原理。 #### 三、模拟实战训练 通过历年试题进行反复演练是非常有效的备考方式之一。虽然现在无法获取到最新的2024年具体题目,但是可以通过分析往年的考题来预测可能出现的新颖考点,并针对性加强薄弱环节。 --- ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值