第十一届蓝桥杯大赛软件类省赛第二场 C/C++ 大学 B 组 题面和题解

文章目录[x]
  1. 1:题面:
  2. 2:思路:
  3. 3:代码:
  4. 4:答案:
  5. 5:思路:
  6. 6:代码:
  7. 7:答案:
  8. 8:题面:
  9. 9:思路:
  10. 10:代码:
  11. 11:答案:
  12. 12:题面:
  13. 13:思路:
  14. 14:代码:
  15. 15:答案:
  16. 16:题面:
  17. 17:思路:
  18. 18:代码:
  19. 18.1:建图代码:
  20. 18.2:DFS求联通块代码
  21. 19:题面:
  22. 20:思路:
  23. 21:代码:




C

A:门牌制作

题面:

在这里插入图片描述

思路:

暴力遍历,1-2020的数字,然后判断每一位,计数。

代码:

/*
 * @Date: 2020-10-17 21:49:55
 * @LastEditTime: 2020-10-17 21:58:58
 * @Author's blog: blog.nuoyanli.com
 * @Description: Plum blossom from the bchter cold!
 */
#include <bits/stdc++.h>
using namespace std;
int main() {
  int ans = 0;
  for (int i = 1; i <= 2020; i++) {
    int j = i;
    while (j) {
      if (j % 10 == 2){
        ans++;
      }
      j /= 10;
    }
  }
  cout << ans << endl;
  return 0;
}

答案:

624

B:既约分数

在这里插入图片描述

思路:

遍历1-2020之间的任意点对,判断gcd是否为1,计数。

代码:

/*
 * @Date: 2020-10-17 22:02:12
 * @LastEditTime: 2020-10-17 22:02:24
 * @Author's blog: blog.nuoyanli.com
 * @Description: Plum blossom from the bchter cold!
 */
#include <bits/stdc++.h>
using namespace std;

int main() {
  int ans = 0;
  for (int i = 1; i <= 2020; i++) {
    for (int j = 1; j <= 2020; j++) {
      if (__gcd(i, j) == 1){
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

答案:

2481215

C:蛇形填数

题面:

在这里插入图片描述

思路:

模拟整个过程,判断一下奇偶位置的特殊情况。

代码:

/*
 * @Date: 2020-10-17 22:05:26
 * @LastEditTime: 2020-10-17 22:06:55
 * @Author's blog: blog.nuoyanli.com
 * @Description: Plum blossom from the bchter cold!
 */
#include <bits/stdc++.h>
using namespace std;
int mmp[50][50], id;
int main() {
  for (int i = 1; i <= 40; i++) {
    if (i & 1) {
      int idx = i, idy = 1;
      for (int j = 0; j < i; j++){
        mmp[idx - j][idy + j] = ++id;
      }
    } else {
      int idx = 1, idy = i;
      for (int j = 0; j < i; j++){
        mmp[idx + j][idy - j] = ++id;
      }
    }
  }
  cout << mmp[20][20] << endl;
  return 0;
}

答案:

761

D:跑步锻炼

题面:

在这里插入图片描述

思路:

直接模拟日期加法,递增到2020年10月1日为止,计数(注意第一天也是2哦),注意判断闰年,处理好年月日。

代码:

/*
 * @Date: 2020-10-17 22:09:57
 * @LastEditTime: 2020-10-17 22:11:55
 * @Author's blog: blog.nuoyanli.com
 * @Description: Plum blossom from the bchter cold!
 */

#include <bits/stdc++.h>
using namespace std;
int M[13] = {0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
  int y = 2000, m = 1, d = 1, w = 6, ans = 2;
  while (y != 2020 || m != 10 || d != 1) {
    if (y % 400 == 0 || (y % 4 == 0 && y % 100)) {
      M[2] = 29;
    } else {
      M[2] = 28;
    }
    d++;
    w = (w + 1) % 7;
    if (d > M[m]) {
      d = 1;
      m++;
      if (m > 12) {
        m = 1;
        y++;
      }
    }
    if (d == 1 || w == 1) {
      ans++;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}

答案:

8979

E:七段码

题面:

在这里插入图片描述

思路:

暴力枚举次方的所有七段码,然后DFS判断联通块。(不愧是暴力杯

代码:

建图代码:

/*
 * @Date: 2020-10-17 21:20:30
 * @LastEditTime: 2020-10-17 22:15:57
 * @Author's blog: blog.nuoyanli.com
 * @Description: Plum blossom from the bchter cold!
 */
#include <bits/stdc++.h>
using namespace std;
int main() {
  int ans = 0;
  freopen("in.txt", "w", stdout);
  for (int a = 0; a <= 1; a++) {
    for (int b = 0; b <= 1; b++) {
      for (int c = 0; c <= 1; c++) {
        for (int d = 0; d <= 1; d++) {
          for (int e = 0; e <= 1; e++) {
            for (int f = 0; f <= 1; f++) {
              for (int g = 0; g <= 1; g++) {
                int mmp[6][4] = {0};
                if (a) {
                  for (int i = 1; i <= 3; i++) {
                    mmp[1][i] = 1;
                  }
                }
                if (b) {
                  for (int i = 1; i <= 3; i++) {
                    mmp[i][3] = 1;
                  }
                }
                if (c) {
                  for (int i = 3; i <= 5; i++) {
                    mmp[i][3] = 1;
                  }
                }
                if (d) {
                  for (int i = 1; i <= 3; i++) {
                    mmp[5][i] = 1;
                  }
                }
                if (e) {
                  for (int i = 3; i <= 5; i++) {
                    mmp[i][1] = 1;
                  }
                }
                if (f) {
                  for (int i = 1; i <= 3; i++) {
                    mmp[i][1] = 1;
                  }
                }
                if (g) {
                  for (int i = 1; i <= 3; i++) {
                    mmp[3][i] = 1;
                  }
                }
                printf("5 3\n");
                for (int i = 1; i <= 5; i++) {
                  for (int j = 1; j <= 3; j++) {
                    printf("%d", mmp[i][j]);
                  }
                  puts("");
                }
                puts("");
              }
            }
          }
        }
      }
    }
  }
  return 0;
}

DFS求联通块代码

/*
 * @Date: 2020-10-17 21:20:03
 * @LastEditTime: 2020-10-17 21:27:00
 * @Author's blog: nuoyanli
 * @Description: Plum blossom from the bchter cold!
 */
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
char a[N][N];
int n, m, vis[N][N];

void dfs(int r, int c, int f) {
  if (r < 0 || r >= m || c < 0 || c >= n) {
    return;
  }
  if (vis[r][c] > 0 || a[r][c] != '1') {
    return;
  }
  vis[r][c] = f;
  for (int i = -1; i <= 1; i++){
    for (int j = -1; j <= 1; j++){
      if (i != 0 || j != 0){
        dfs(r + i, c + j, f);
      }
    }
  }
}

int main() {
  int sum = 0;
  freopen("in.txt", "r", stdin);
  while (~scanf("%d%d", &m, &n)) {
    for (int i = 0; i < m; i++){
      scanf("%s", a[i]);
    }
    memset(vis, 0, sizeof(vis));
    int cnt = 0;
    for (int i = 0; i < m; i++){
      for (int j = 0; j < n; j++){
        if (vis[i][j] == 0 && a[i][j] == '1'){
          dfs(i, j, ++cnt);
        }
      }
    }
    if (cnt == 1){
      sum += 1;
    }
  }
  printf("%d\n", sum);
  return 0;
}

F:成绩统计

题面:

在这里插入图片描述
在这里插入图片描述

思路:

直接计算,然后注意四舍五入。

代码:

/*
 * @Date: 2020-10-17 22:18:36
 * @LastEditTime: 2020-10-17 22:21:48
 * @Author's blog: blog.nuoyanli.com
 * @Description: Plum blossom from the bchter cold!
 */
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, jg = 0, yx = 0;
  scanf("%d", &n);
  while (n--) {
    int x;
    scanf("%d", &x);
    if (x >= 60) {
      jg++;
    }
    if (x >= 85) {
      yx++;
    }
  }
  double Jg = jg * 1.0 / n * 1.0;
  double Yx = yx * 1.0 / n * 1.0;
  int ans_jg = int(Jg * 1000 + 5) / 10;
  int ans_yx = int(Yx * 1000 + 5) / 10;
  printf("%d%%\n%d%%\n", ans_jg, ans_yx);
  return 0;
}


点赞

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像