2020 第十一屆藍橋杯大賽軟體類省賽第二場 C/C++ 大學 B 組 題面和部分題解

2020-10-19 16:00:17

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;
}

答案:

8879

E:七段碼

題面:

在這裡插入圖片描述

思路:

暴力列舉 2 7 = 128 2^7=128 27=128次方的所有七段碼,然後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;
}

H:子串分值和

題面:

在這裡插入圖片描述

思路:

算單字母的貢獻,然後記錄一下上一個位置。

程式碼:

/*
 * @Date: 2020-10-18 13:13:49
 * @LastEditTime: 2020-10-18 15:26:45
 * @Author's blog: blog.nuoyanli.com
 * @Description: Plum blossom from the bchter cold!
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
char s[N];
ll vis[40];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (i - vis[s[i] - 'a']) * (n - i + 1);
    vis[s[i] - 'a'] = i;
  }
  cout << ans << endl;
  return 0;
}