优秀的编程知识分享平台

网站首页 > 技术文章 正文

C++ GESP 四级 2025年3月 真题及解析

nanyue 2025-06-08 23:11:44 技术文章 27 ℃

1

单项选择题



2

判断题



3

编程实战


荒地开垦


题目分析

  1. 统计原本可以开垦的荒地数量
  2. 枚举清除每一块#后可以增加的荒地最大数量
  3. 结果为上述两部分之和。


参考程序

#include<bits/stdc++.h>
usingnamespacestd;


constint N = 1010;
int n, m;
char g[N][N];
// 上右左下
int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};
boolcheck(int x, int y, int d){
  for(int i = 0; i < 4; i++){
    if(i == 3 - d) continue;
    int nx = x + dx[i], ny = y + dy[i];
    if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && g[nx][ny] == '#')
      returnfalse;
  }
  returntrue;
}
intmain(){
  cin >> n >> m;
  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
      cin >> g[i][j];
  // 统计原本可以开垦的荒地数量
  int cnt = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
      if(g[i][j] == '#') continue; // 剔除自己是杂物情况
      bool is_ok = true;
      for(int k = 0; k < 4; k++){
        int nx = i + dx[k], ny = j + dy[k];
        if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && g[nx][ny] == '#'){
          is_ok = false;
          break;
        }
      }
      cnt += is_ok;
    }
  }
  // cout << cnt << endl;
  // cout << check(1, 2, 0);
  // 枚举清除每一块#后可以增加的荒地最大数量
  int maxv = 0;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= m; j++){
      if(g[i][j] == '#'){
        int t = 0;
        bool is_ok = true; // 中间点是否可以统计进去
        for(int k = 0; k < 4; k++){
          int nx = i + dx[k], ny = j + dy[k];
          if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && g[nx][ny] == '#'){
            is_ok = false;
            break;
          }
        }
        
        t += is_ok;
        // cout << "1:" << t << endl;
        // 上
        if(i - 1 >= 1 && g[i - 1][j] == '.' && check(i - 1, j, 0))
          t++;
        
        // cout << "2:" << t << endl;
        // 下
        if(i + 1 <= n && g[i + 1][j] == '.' && check(i + 1, j, 3))
          t++;
        // cout << "3:" << t << endl;
        // 左
        if(j - 1 >= 1 && g[i][j - 1] == '.' && check(i, j - 1, 2))
          t++;
        // cout << "4:" << t << endl;
        // 右
        if(j + 1 <= m && g[i][j + 1] == '.' && check(i, j + 1, 1))
          t++;
          
        // cout << "5:" << t << endl;
        // printf("(%d,%d)  %d\n", i, j, t);
        maxv = max(maxv, t);
      }
      
    }
  }
  // cout << cnt << maxv << endl;
  cout << cnt + maxv;
  return0;
}


二阶矩阵


题目分析

简单题,枚举法。


参考程序

#include<bits/stdc++.h>
usingnamespacestd;


constint N = 510;


int n, m;
int a[N][N];


intmain(){


  cin >> n >> m;


  for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
      cin >> a[i][j];


  int cnt = 0;
  for(int i = 1; i < n; i++){
    for(int j = 1; j < m; j++){
      int t1 = a[i][j], t2 = a[i + 1][j + 1], t3 = a[i][j + 1], t4 = a[i + 1][j];


      if(t1 * t2 == t3 * t4)
        cnt++;
    }
  }


  cout << cnt;
  
  return0;
}

Tags:

最近发表
标签列表