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

摘要

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

1-5 编程解决一切

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


int main(void){
cout<<"Problem? The Solution: Programming.";
return 0;
}

2-5 再进去几个人

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

int main(void){
int a,b;
cin>>a>>b;
cout<<b-a;
return 0;
}

3-10 帮助色盲

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
#include<bits/stdc++.h>
using namespace std;

// 输入在一行中给出两个数字 A 和 B,其间以空格分隔。
// 其中 A 是当前交通灯的颜色,取值为 0 表示红灯、1 表示绿灯、2 表示黄灯;
// B 是前方行人的状态,取值为 0 表示前方两米内没有同向行走的人、1 表示有。

// 根据输入的状态在第一行中输出提示音:
// dudu 表示前方为绿灯,可以继续前进;
// biii 表示前方为红灯,应该止步;
// - 表示不做提示。在第二行输出患者应该执行的动作:
// move 表示继续前进、stop 表示止步。

int main(void){
int l,p;
cin>>l>>p;
if(l == 2){
cout<<"-\n";
cout<<"stop";
}else if(p == 1){
cout<<"-\n";
if(l == 1) cout<<"move";
else if(l == 0) cout<<"stop";
}else{
if(l == 1) cout<<"dudu\nmove";
else if(l == 0) cout<<"biii\nstop";
}
return 0;
}

4-10 四项全能

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

int n,m,sum;

int main(void){
cin>>n>>m;
for(int i = 1 ; i <= m ; i ++){
int x;cin>>x;
sum += x;
}
cout<<max(0, sum - (m-1)*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
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
using namespace std;

int n,k,cnt;
vector<string> v;
string str;

void solve(string s){
string res = "";
int len = s.length();
for(int i = 0 ; i < str.length() ; i ++){
if(i + len - 1 < str.length() && str.substr(i, len) == s){
res += "_";
i = i+len-1;
cnt++;
}else{
res += str[i];
}
}
str = res;
}

int main(void){
cin>>n;
for(int i = 1 ; i <= n ; i ++){
string s;cin>>s;
v.push_back(s);
}
cin>>k;
getchar();
getline(cin, str);
for(auto it : v){
solve(it);
}
if(cnt >= k){
cout<<cnt<<"\n"<<"He Xie Ni Quan Jia!";
}else{
for(auto it : str){
if(it == '_') cout<<"<censored>";
else cout<<it;
}
}
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
#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;

int n,cnt[maxn];
double sum,p[maxn];

int main(void){
cin>>n;
for(int i = 1 ; i <= n ; i ++){
cin>>p[i];
}
int t,num;
while(cin>>t>>num, t != 0){
sum += p[t]*num;
cnt[t] += num;
}
for(int i = 1 ; i <= n ; i ++){
cout<<cnt[i]<<"\n";
}
cout<<fixed<<setprecision(2)<<sum;
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
using namespace std;

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

int solve(int x){
int cnt = 0;
while(x > 10){
int nex = 1;
int temp = x;
while(temp > 0){
nex *= temp%10;
temp /= 10;
}
x = nex;
cnt++;
}
return cnt;
}

int main(void){
cin>>a>>b;
for(int i = a ; i <= b ; i ++){
int cnt_i = solve(i);
if(cnt_i > max_){
max_ = cnt_i;
ans.clear();
ans.push_back(i);
}else if(cnt_i == max_){
ans.push_back(i);
}
}
cout<<max_<<"\n";
for(int i = 0 ; i < ans.size(); i ++){
cout<<ans[i]<<(i == ans.size()-1 ? "\n":" ");
}
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;

int t, a[10][10];
bool vis[10];

bool judge(){
for(int i = 1 ; i <= 9 ; i ++){
for(int j = 1 ; j <= 9 ; j ++){
if(!(a[i][j] >= 1 && a[i][j] <= 9)) return false;
}
}
for(int i = 1 ; i <= 9 ; i ++){
memset(vis,false,sizeof(vis));
for(int j = 1 ; j <= 9 ; j ++){
if(vis[a[i][j]]) return false;
vis[a[i][j]] = true;
}
}
for(int i = 1 ; i <= 9 ; i ++){
memset(vis,false,sizeof(vis));
for(int j = 1 ; j <= 9 ; j ++){
if(vis[a[j][i]]) return false;
vis[a[j][i]] = true;
}
}
for(int i = 1 ; i <= 7 ; i += 3){
for(int j = 1 ; j <= 7 ; j += 3){
memset(vis,false,sizeof(vis));
for(int x = i ; x <= i + 2 ; x ++){
for(int y = j ; y <= j + 2; y ++){
if(vis[a[x][y]]) return false;
vis[a[x][y]] = true;
}
}
}
}
return true;
}

int main(void){
cin>>t;
while(t--){
for(int i = 1 ; i <= 9 ; i ++){
for(int j = 1 ; j <= 9 ; j ++){
cin>>a[i][j];
}
}
if(judge()) cout<<1<<"\n";
else cout<<0<<"\n";
}
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
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;

int n,m,q;
vector<int > v[maxn];

int main(void){
cin>>n>>m;
for(int i = 1 ; i <= n ; i ++){
int k;cin>>k;
for(int j = 1 ; j <= k ; j ++){
int x;cin>>x;
v[x].push_back(i);
}
}
cin>>q;
while(q--){
int a,b; cin>>a>>b;
int res = 0;
for(auto it : v[a]){
for(auto jt : v[b]){
if(it == jt){
res++;
}
}
}
cout<<res<<"\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;

unordered_map<string, vector<string> > mp;
int n,q;

string get_head(string s){
string res = "";
string t = " " + s;
for(int i = 1 ; i < t.length() ; i ++){
if(t[i-1] == ' '){
while(i < t.length() && t[i] == ' ') i++;
res += t[i];
}
}
return res;
}

int main(void){
cin>>n;getchar();
for(int i = 1 ; i <= n ; i ++){
string s;
getline(cin, s);
string h = get_head(s);
mp[h].push_back(s);
}
cin>>q;
getchar();
while(q--){
string s;
getline(cin, s);
string h = get_head(s);
if(mp.count(h) != 0){
vector<string> v = mp[h];
sort(v.begin(), v.end());
for(int i = 0 ; i < v.size(); i ++){
cout<<v[i]<<(i == v.size()-1 ? "\n": "|");
}
}else{
cout<<s<<"\n";
}
}
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
41
42
43
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;

vector<int > e[maxn];
int n,r,d;

bool judge(){
int i = 1;
while(i <= n && e[i].size() == 0) i++;
for(int j = i+1 ; j <= n ; j ++){
if(e[j].size() != 0 && e[j].size() != e[i].size()){
return false;
}
}
return true;
}

void dfs(int x){
if(x != r) cout<<" ";
cout<<x;
for(auto it : e[x]){
dfs(it);
}
}

int main(void){
cin.tie(0),cout.tie(0);
ios::sync_with_stdio(false);
cin>>n;
for(int i = 1 ; i <= n ; i ++){
int f;cin>>f;
if(f == 0) r = i;
else e[f].push_back(i);
d = max((int)e[f].size(), d);
}
cout<<d<<" "<<(judge() ? "yes\n":"no\n");
for(int i = 1 ; i <= n ; i ++){
sort(e[i].begin(), e[i].end());
}
dfs(r);
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
47
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10;

int l,n,ans;
int a[maxn][maxn], row[maxn], col[maxn];

bool judge(){
for(int i = 1 ; i <= n ; i ++){
if(row[i] != l || col[i] != l) return false;
}
return true;
}

void dfs(int x, int y){
for(int i = 1 ; i <= n ; i ++){
if(col[i] > l || row[i] > l) return;
}
if(x == n && y == n){
for(int i = 0 ; i <= l; i ++){
a[x][y] = i;
row[x] += i; col[y] += i;
if(judge()) ans++;
row[x] -= i; col[y] -= i;
}
return;
}
for(int i = 0 ; i <= l; i ++){
a[x][y] = i;
if(y == n){
row[x] += i; col[y] += i;
if(row[x] == l) dfs(x + 1, 1);
row[x] -= i; col[y] -= i;
}else{
row[x] += i; col[y] += i;
if(!(x == n && col[y] != l)) dfs(x, y + 1);
row[x] -= i; col[y] -= i;
}
}
}

int main(void){
cin>>l>>n;
dfs(1,1);
cout<<ans;
return 0;
}

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