当前位置:主页 > 查看内容

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

发布时间:2021-06-21 00:00| 位朋友查看

简介:2020第十一届11月蓝桥杯大赛软件类B组C/C省赛目录 试题 A门牌制作结果填空 试题 B既约分数结果填空 试题 C蛇形填数结果填空 试题 D跑步锻炼结果填空 试题 E七段码结果填空 试题 F成绩统计程序设计 试题 G回文日期结果填空 试题 H子串分值和程序设计 试题 A门……

试题 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) (i?pre[s[i]?a])?(len?i+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;
}
;原文链接:https://blog.csdn.net/qq_43408978/article/details/114634945
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐