GPLT团体程序设计天梯赛真题训练:2023年

摘要

编程题 - 题目列表 - 2023 天梯赛

题目 分值 知识点
什么是机器学习 5 Hello World
程序员买包子 10 分支
进化论 10 分支
猜帽子游戏 15 分支
剪切粘贴 15 字符串处理
分寝室 20 简单模拟
谁管谁叫爹 20 简单模拟
堆宝塔 25 数据结构
天梯赛的赛场安排 25 简单模拟
锦标赛 25 二叉树
寻宝图 25 DFS

1-5 最好的文档

1
2
3
4
5
6
7
#include<bits/stdc++.h>
using namespace std;

int main(void){
cout<<"Good code is its own best documentation.";
return 0;
}

2-5 什么是机器学习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<bits/stdc++.h>
using namespace std;

int a, b;
int ans;

int main(void){
cin>>a>>b;
ans = a + b;
cout<<ans-16<<endl;
cout<<ans-3<<endl;
cout<<ans-1<<endl;
cout<<ans;
return 0;
}

3-10 程序员买包子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<bits/stdc++.h>
using namespace std;

string x;
int n,m,k;

int main(void){
cin>>n>>x>>m>>k;
if(k == n){
cout<<"mei you mai "<<x<<" de";
}else if(k == m){
cout<<"kan dao le mai "<<x<<" de";
}else{
cout<<"wang le zhao mai "<<x<<" de";
}
return 0;
}

4-10 进化论

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<bits/stdc++.h>
using namespace std;

int t;

int main(void){
cin>>t;
while(t--){
int a,b,c;
cin>>a>>b>>c;
if(a*b == c){
cout<<"Lv Yan\n";
}else if(a + b == c){
cout<<"Tu Dou\n";
}else{
cout<<"zhe du shi sha ya!\n";
}
}
return 0;
}

5-15 猜帽子游戏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;

int n,t;
int a[maxn],b[maxn];

void solve(){
bool f = false;
int w = 0;
for(int i = 1 ; i <= n ; i ++){
cin>>b[i];
if(b[i] != 0) f = true;
if(b[i] != 0) w += (b[i] != a[i]);
}
if(f && w == 0){
cout<<"Da Jiang!!!\n";
}else{
cout<<"Ai Ya\n";
}
}

int main(void){
cin>>n;
for(int i = 1 ; i <= n ; i ++){
cin>>a[i];
}
cin>>t;
while(t--){
solve();
}
return 0;
}

6-15 剪切粘贴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<bits/stdc++.h>
using namespace std;

string s;
int t;

void solve(){
int a, b; string l, r;
cin>>a>>b>>l>>r;
string cut = s.substr(a-1, b-a+1);
s.erase(a-1, b-a+1);
int p = s.find(l+r);
if(p == -1){
s += cut;
}else{
// s.insert(p + l.size(), cut);
s = s.substr(0, p + l.size()) + cut + s.substr(p+l.size());
}
}

int main(void){
cin>>s>>t;
while(t--){
solve();
}
cout<<s;
return 0;
}

7-20 分寝室

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<bits/stdc++.h>
using namespace std;

int n0,n1,n;
int mid,ans;

int main(void){
cin>>n0>>n1>>n;
mid = INT_MAX;
ans = -1;
for(int i = 1 ; i < n ; i ++){
int a = i, b = n-i;
if(n0 % a == 0 && n1 % b == 0 && n0 != a && n1 != b){
int new_mid = abs(n0/a - n1/b);
if(new_mid < mid){
mid = new_mid;
ans = i;
}
}
}
if(ans != -1) cout<<ans<<" "<<n-ans;
else cout<<"No Solution";
return 0;
}

8-20 谁管谁叫爹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<bits/stdc++.h>
using namespace std;

int t;

int getNum(int x){
int res = 0;
while(x){
res += x%10;
x /= 10;
}
return res;
}

void solve(){
int na,nb;cin>>na>>nb;
int sa = getNum(na);
int sb = getNum(nb);
if(na % sb == 0 && nb % sa != 0){
cout<<"A";
}else if(na % sb != 0 && nb % sa == 0){
cout<<"B";
}else{
if(na > nb) cout<<"A";
else cout<<"B";
}
cout<<"\n";
}

int main(void){
cin>>t;
while(t--){
solve();
}
return 0;
}

9-25 堆宝塔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;

vector<int > a,b;
vector<vector<int > > ans;

int t;
int h;

void solve(){
int x; cin>>x;
if(a.empty() || x < a.back()){
a.push_back(x);
}else if(b.empty() || x > b.back()){
b.push_back(x);
}else{
ans.push_back(a);
a.clear();
while(b.size() && b.back() > x){
a.push_back(b.back());
b.pop_back();
}
a.push_back(x);
}
}

void End(){
if(a.size()){
ans.push_back(a);
a.clear();
}
while(b.size()){
a.push_back(b.back());
b.pop_back();
}
if(!a.empty()){
ans.push_back(a);
a.clear();
}
for(auto v : ans){
h = max(h, (int)v.size());
}
}

int main(void){
cin>>t;
while(t--){
solve();
}
End();
cout<<ans.size()<<" "<<h<<"\n";
return 0;
}

10-25 天梯赛的赛场安排

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
using namespace std;


vector<int > v;
priority_queue<pair<int, string> > pq;
map<string, int> mp;
vector<string > out;
int n,c;

int main(void){
cin>>n>>c;
for(int i = 1 ; i <= n ; i ++){
string s; int m;
cin>>s>>m;
pq.push({m, s});
out.push_back(s);
}
while(!pq.empty()){
int m = pq.top().first;
string s = pq.top().second;
pq.pop();
if(m >= c){
if(m-c != 0) pq.push({m-c, s});
v.push_back(c);
}else{
bool f = false;
for(int i = 0 ; i < v.size() ; i ++){
if(m + v[i] <= c){
v[i] += m;
f = true;
break;
}
}
if(!f) v.push_back(m);
}
mp[s]++;
}
for(auto s : out){
cout<<s<<" "<<mp[s]<<"\n";
}
cout<<v.size();
return 0;
}

11-25 锦标赛

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
using namespace std;
const int maxn = (1<<20);

struct game{
int win, lose;
}g[maxn];
int k;

bool judge(int x){
if(x >= (1<<k)) return true;
if(g[x].win < g[x].lose) return false;

g[(x<<1)].win = g[x].win;
g[(x<<1) + 1].win = g[x].lose;
if(judge(x<<1) && judge((x<<1) + 1)) return true;

g[(x<<1)].win = g[x].lose;
g[(x<<1) + 1].win = g[x].win;
if(judge(x<<1) && judge((x<<1) + 1)) return true;

return false;
}

int main(void){
cin>>k;
for(int i = k ; i >= 1 ; i --){
for(int j = 1<<(i-1); j < (1<<i) ; j ++){
cin>>g[j].lose;
}
}
cin>>g[1].win;
if(!judge(1)) cout<<"No Solution";
else{
for(int i = (1<<(k-1)) ; i < (1<<k) ; i ++){
cout<<g[i].win<<" "<<g[i].lose<<" ";
}
}
return 0;
}

12-25 寻宝图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
using namespace std;
const int maxm = 1e5 + 10;

int n,m;
vector<char > v[maxm];
vector<bool> vis[maxm];
const int dirx[4] = {1, -1, 0, 0};
const int diry[4] = {0, 0, 1, -1};
int cnt, ans;
bool ans_once;

void dfs(int x, int y){
vis[x][y] = true;
if(v[x][y] >= '2' && v[x][y] <= '9') ans_once = true;
for(int i = 0 ; i < 4 ; i ++){
int nx = x + dirx[i];
int ny = y + diry[i];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny] && v[nx][ny] != '0'){
dfs(nx, ny);
}
}
}

int main(void){
cin>>n>>m;
for(int i = 1 ; i <= n ; i ++){
v[i].resize(m+1);
vis[i].resize(m+1);
for(int j = 1 ; j <= m ; j ++){
cin>>v[i][j];
}
}
for(int i = 1 ; i <= n ; i ++){
for(int j = 1 ; j <= m ; j ++){
if(!vis[i][j] && v[i][j] != '0'){
ans_once = false;
dfs(i, j);
cnt++;
if(ans_once) ans++;
}
}
}
cout<<cnt<<" "<<ans;
return 0;
}

GPLT团体程序设计天梯赛真题训练:2023年
https://czwcugb.github.io/题解/GPLT/2023/
作者
ChenZhiwei
发布于
2025年4月6日
许可协议