【ACGO】2024.10社区邀请赛题解

引言

本文章的讲解有关于【代码修炼手册团队】2024.10社区邀请赛竞赛的题目,邀请码为ba3n,欢迎各位前来尝试。若有意愿参加团队的,可以点击这里:中国团附属:代码修炼手册。谢谢大家!


目录

T1 天安门广场

T2 升旗仪式

T3 阅兵

T4 统计雪糕数

T5 离开前的数学题

T6 完善程序题模拟:走迷宫


T1 天安门广场

本题目仅为一道输出题,大家通过网络查询即可得到正确答案:34.7。代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
    cout << "34.7";
    return 0;
}

T2 升旗仪式

仍然是通过网络查询,可得国旗杆的长度为30m,因为国旗不可能一直向上升,到达了30m时就会停止。此时我们仅需要加入一个特判,判断国旗的高度是否到达了30m,如果到达输出0;否则输出总高度30m减去升起来的高度。由公式s=vt可得升起的高度即为n*m。代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m; cin >> n >> m;
    if(n * m >= 30) cout << 0;
    else cout << 30 - n * m;
    return 0;
}

T3 阅兵

此题很简单,但是需要注意使用getline()函数读取。因为输入可能含有空格,使用getline()可以将空格输入进去。直接通过(int)(text)的方式就可以直接将字符转换为ACSII码了。代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s; getline(cin,s);
    int l = s.size();
    for(int i = 0;i < l;i++){
        cout << (int)s[i] << " ";
    }
    return 0;
}

T4 统计雪糕数

首先,在输入时请输入成一个字符串,便于进行遍历提取;同时,在提取时直接通过map中增加内容的特性(通过键访问值,自动排序),数每种雪糕的个数,最后通过auto迭代器输出每个内容即可(first访问键,second访问值)。代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s; cin >> s;
    int l = s.size();
    map<char,int> mp;
    for(int i = 0;i < l;i++) mp[s[i]]++;
    for(auto i : mp) cout << i.first << ":" << i.second << endl;
    return 0;
}

T5 离开前的数学题

这道题实际上是本套竞赛的压轴题。我们为了比较,可以先写一个函数用于比较a和b字符串的大小(注意长度)。由于字符串可以提出一段的特性,在输入时使用string;往后进行比较时,将此时的字符串与前一个字符串进行比较。如果比较大就输出,并把maxx(前一个字符串)设为now(当前字符串)再清空now字符串;如果比maxx小,就保留至在下一次循环拼接。为了去除前导0,直接使用stoi()函数即可(由于本题数据较小,stoi即可完成),最后输出剩余的字符串即可(若长度为0不输出)。代码如下:

#include<bits/stdc++.h>
using namespace std;
int find_maxone(string a,string b){
    if(a.size() != b.size()){
        if(a.size() > b.size()) return 1;
        else return 2;
    }else if(a > b) return 1;
    else return 2;
}
int main(){
    string s; cin >> s;
    int l = s.size();
    string now = "",maxx = "";
    for(int i = 0;i < l;i++){
        now += s[i];
        if(find_maxone(now,maxx) == 1){
            maxx = now;
            cout << stoi(now) << " ";
            now = "";
        }
    }
    if(now.size() > 0) cout << stoi(now);
    return 0;
}

T6 完善程序题模拟:走迷宫

这题就是一个普通的广搜,套模板即可(请勿加空格)。代码如下:

//试卷代码
#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y,step;
};
int n,m;
bool vis[45][45];
char a[45][45];
int arr[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
bool check(int x,int y){
    return x >= 1 && y >= 1 && x <= n && y <= m && vis[x][y] == false && a[x][y] != '#';
}
int bfs(int x,int y,int step){
    queue<node> q;
    q.push({x,y,step});
    vis[x][y] = true;
    while(!q.empty()){
        node t = q.front();
        q.pop();
        if(t.x == n && t.y == m){
            return t.step + 1;
        }
        for(int i = 0;i < 4;i++){
            int nx = t.x + arr[i][0];
            int ny = t.y + arr[i][1];
            if(check(nx,ny)){
                vis[nx][ny] = true;
                q.push({nx,ny,t.step + 1});
            }
        }
    }
}
int main(){
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            cin >> a[i][j];
        }
    }
    cout << bfs(1,1,0);
    return 0;
}

//答题卡代码
#include<bits/stdc++.h>
using namespace std;
int main(){
    cout << "C"; //第一题
    cout << "B"; //第二题
    cout << "D"; //第三题
    cout << "A"; //第四题
    cout << "A"; //第五题
    return 0;
}

总结

总结一下,本次竞赛难度较低。T1的知识点为输入输出,T2的知识点为分支判断,T3的知识点为字符,T4的知识点为STL哈希表(桶排序也可以),T5的知识点为字符串,最后一题T6的知识点为广搜。本次报名人数较少,期待接下来各位的发挥!


竞赛预告

本团队将会在2025.1将会举办全平台挑战赛,难度极其之高。大神们,欢迎各位的参加!具体日期及报名方法将会在接下来的文章中一一介绍,欢迎各位的参加!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值