当前位置: 首页 > article >正文

GESP C++ 四级 编程题 洛谷习题集

O(∩_∩)O哈哈 ,我又回来了

这次打算做洛谷题库中的习题 (这不马上就要考C++四级了嘛)

四级大纲参考:

四级大纲1

2

3

这次习题有点混合,大家适当食用!全部都是跟四级考点有关的!

直接进入正文——


P4325 [COCI2006-2007#1] Modulo

考点:排序+枚举

这道题十分水,直接存入set容器自动排序 (不对,我为什么会在诸位大佬面前说这种低级话语)

#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	vector<ll> a(11, 0);
	set<ll> s;
	for (ll i = 1; i <= 10; i++) {
		cin >> a[i];
		s.insert(a[i] % 42);
	}
	cout << s.size() << endl;
	return 0;
}

P7614 [COCI2011-2012#2] NAJBOLJIH 5

考点:枚举+排序

用pair做是不是更简单呢

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;

pair<ll, ll> a[10];

inline bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
	return a.second < b.second;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	ll ans = 0;
	for (ll i = 1; i <= 8; i++) {
		cin >> a[i].first;
		a[i].second = i;
	}
	//排序(值)
	sort(a + 1, a + 9);
	for (ll i = 4; i <= 8; i++) {
		ans += a[i].first;
	}
	cout << ans << endl;
	//排序(标号)
	sort(a + 4, a + 9, cmp);
	for (ll i = 4; i <= 8; i++) {
		cout << a[i].second << " ";
	}
	return 0;
}

P2813 母舰

考点:模拟+排序+贪心

这基本上就是排序常考知识点了 (居然还是广东的)

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
ll m, n, s, d;
ll df[10000000], wf[10000000];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin >> m >> n;
	for (ll i = 1; i <= m; i++) cin >> df[i];
	for (ll i = 1; i <= n; i++) cin >> wf[i];
	sort(df + 1, df + 1 + m);
	sort(wf + 1, wf + 1 + n);
	d = 1;
	for (ll i = 1; i <= n; i++) {
		if (df[d] == 0) d++;
		if (df[d] < wf[i] && df[d] != 0) {
			wf[i] = 0;
			d++;
		}
	}
	if (d <= m) {
		cout << 0 << endl;
		return 0;
	}
	for (ll i = 1; i <= n; i++) s += wf[i];
	cout << s << endl;
	return 0;
}

P4305 [JLOI2011] 不重复数字

考点:排序+哈希(亿点点……)

这道题是省选题(吉林省选)不过这也太shui了吧……unordered_set即可完成去重不排序的结果!

#include <iostream>
#include <vector>
#include <unordered_set>
#include <cmath>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ll T;
    cin >> T;
    while (T--) {
        ll n;
        cin >> n;
        vector<ll> a(n + 1, 0);
        unordered_set<ll> seen; // 用于记录已经见过的元素
        for (ll i = 1; i <= n; i++) {
            cin >> a[i];
            // 如果这个元素还没有被见过,则输出它并标记为已见
            if (seen.find(a[i]) == seen.end()) {
                cout << a[i] << " ";
                seen.insert(a[i]);
            }
        }
        cout << endl;
    }

    return 0;
}

好了,排序的题目刷完了,来“吃”点“大餐”——真题

链接:CCF GESP C++ 四级上机题

由于一些已做过,我就只做上几道即可

B3870 [GESP202309 四级] 变长编码

考点:字符串+模拟+进制

这道题去年9月份的,虽说不难,但也不水……简直就可以概括成一句话:位操作的基本应用

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;

ll n;

void print(ll x) {
	if (x <= 9) cout << x;
	else cout << char(x - 10 + 'A');
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin >> n;
	if (n == 0) {
		cout << "00";
		return 0;
	}
	while (n) {
		ll t = n & 0x7f;
		n >>= 7;
		if (n > 0) t |= 0x80;
		print((t >> 4) & 0xf);
		print(t & 0xf);
		cout << " ";
	}
	return 0;
}

B3927 [GESP202312 四级] 小杨的字典

考点:字符串+模拟

纯字符串模拟题目

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

// 翻译函数
static string fy(const string& t, const map<string, string>& dictionary) {
    auto it = dictionary.find(t);
    if (it != dictionary.end()) {
        return it->second;
    }
    return "UNK"; // 如果没有找到对应的翻译,返回 "UNK"
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // 输入
    int n;
    cin >> n;
    map<string, string> dictionary; // 存储 A 语言到 B 语言的映射
    for (int i = 1; i <= n; i++) {
        string a, b;
        cin >> a >> b;
        dictionary[a] = b;
    }

    // 忽略行末的换行符
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    // 读取文章并翻译
    string c;
    getline(cin, c); // 读取一行输入

    // 临时存储当前单词
    string t;
    for (char ch : c) {
        if (ch >= 'a' && ch <= 'z') {
            t += ch; // 如果是字母,添加到当前单词
        }
        else {
            if (!t.empty()) { // 如果当前单词非空
                cout << fy(t, dictionary); // 输出翻译后的单词
                t.clear(); // 清空临时单词
            }
            cout << ch; // 输出标点符号
        }
    }
    // 处理最后一个单词
    if (!t.empty()) {
        cout << fy(t, dictionary); // 输出最后一个单词的翻译
    }

    cout << endl; // 输出换行符
    return 0;
}

B4006 [GESP202406 四级] 宝箱

考点:排序+贪心

开始吓到我了,还以为是动态规划(背包),原来就是简单的贪心啊!

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	ll n, k;
	cin >> n >> k;
	vector<ll> a(n + 1, 0);
	for (ll i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	ll ans = 0;
	for (ll i = 1; i <= n; i++) {
		ll sum = 0;
		for (ll j = i; j >= 1; j--) {
			if (a[i] - a[j] <= k) {
				sum += a[j];
			}
			else break;
		}
		ans = max(ans, sum);
	}
	cout << ans << endl;
	return 0;
}

一波AC(满屏绿):

AC

B3959 [GESP202403 四级] 做题

考点:排序+模拟+贪心

没什么好说的(代码十分简单)

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;

ll sum = 1, n, a[1000010];
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; i++) {
		if (a[i] < sum)continue;
		else sum++;
	}
	cout << sum - 1 << endl;
}

B3928 [GESP202312 四级] 田忌赛马

考点:排序+模拟+贪心

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	ll T;
	cin >> T;
	while (T--) {

	}
	return 0;
}

B4005 [GESP202406 四级] 黑白方块

考点:二维数组+模拟+前缀和

居然超纲了!考的是二维数组前缀和!

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAXN = 11;
int n, m;
int grid[MAXN][MAXN];
int preSum[MAXN][MAXN];

// 计算前缀和
void calculatePrefixSum() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1] - preSum[i - 1][j - 1] + grid[i][j];
        }
    }
}

// 获取子矩阵的和
int getSubmatrixSum(int r1, int c1, int r2, int c2) {
    return preSum[r2][c2] - preSum[r1 - 1][c2] - preSum[r2][c1 - 1] + preSum[r1 - 1][c1 - 1];
}

int main() {
    cin >> n >> m;

    // 读取输入
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char c;
            cin >> c;
            grid[i][j] = c - '0';
        }
    }

    calculatePrefixSum();

    int maxBalancedSize = 0;

    // 枚举所有可能的子矩阵
    for (int r1 = 1; r1 <= n; r1++) {
        for (int c1 = 1; c1 <= m; c1++) {
            for (int r2 = r1; r2 <= n; r2++) {
                for (int c2 = c1; c2 <= m; c2++) {
                    int totalCells = (r2 - r1 + 1) * (c2 - c1 + 1);
                    int blackCells = getSubmatrixSum(r1, c1, r2, c2);
                    int whiteCells = totalCells - blackCells;

                    if (blackCells == whiteCells) {
                        maxBalancedSize = max(maxBalancedSize, totalCells);
                    }
                }
            }
        }
    }

    cout << maxBalancedSize << endl;

    return 0;
}

参考习题:

四级

完结撒花!

做题不易,望各位大佬RP++,点个赞,关注一下吧!

再说个悄悄话,对了,我的粉丝破200啦!谢谢大家一直以来对我的关照!点赞争取破500!!!

在这里插入图片描述


http://www.kler.cn/news/284592.html

相关文章:

  • 【JavaScript】JavaScript模块化开发:ES6模块与CommonJs的对比与应用
  • macos 10.15 Catalina 可用docker最新版本 Docker Desktop 4.15.0 (93002) 下载地址与安装方法
  • 5W爆了,建议紧盯这个方向!!
  • OWOD环境配置和训练细节
  • 「OC」初识MVC —— 简单学习UITableView的解耦
  • opencv之阈值处理
  • 网优学习干货:2.6G仿真操作(2)
  • 信息安全--(四)网络安全体系与安全模型(二)
  • Linux系统安装nginx
  • 如何申请 Midjourney API ,看这篇文章就够了
  • Web自动化测试实战--博客系统
  • css中 display block属性的用法
  • docker里装mysql
  • 使用控制台与键盘进行输入输出
  • 服务器数据恢复—光纤共享存储互斥设置不当导致数据丢失的数据恢复案例
  • NC 二分查找-II
  • SQL 中 LIKE 和 REGEXP 的相同点与不同点解析
  • 关于前端布局的基础知识
  • AI绘画【Stable Diffusion】抽卡必备!时间管理大师Agent Scheduler插件,一键设置任务,让你的休息时间充分利用起来!
  • 如何判断请求是否为跨域请求?——详细教程
  • Godot vscode c# 调试方法
  • Linux——用户和权限
  • 代理 IP 在工业物联网中的大作用
  • 自然灾害预警系统的重要性
  • FPGA概述
  • 算法训练营|图论第7天 prim算法 kruskal算法
  • HPM5301系列--VSCODE开发环境问题修复(一)
  • 深度学习100问18:什么是负采样
  • chat2DB体验
  • 安卓蓝牙技术的使用和场景