【码蹄集】— 码蹄杯 2023省赛第一场

MC0201

a + b a+b a+b

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    int a,b; cin >> a >> b;
    cout << a + b << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1;
    while(_ --){
        solve();
    }
    return 0;
}

MC0202

暴力枚举

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    int l,r; char x; cin >> l >> x >> r;
    int sum = 0;
    for(int i = l;i <= r;i ++){
        if(i % 2 || i % 3) continue;
        sum += i;
    }
    cout << sum << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1;
    while(_ --){
        solve();
    }
    return 0;
}

MC0203

map更新分数和最后一次操作的位置即可

然后找最小分数 如果最小分数相同就看更新的位置

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

int n;

void solve(){
    cin >> n;
    map<string,pair<int,int>> mp;
    for(int i = 0;i < n;i ++){
        string s; cin >> s;
        int x; cin >> x;
        mp[s] = {x + mp[s].first,i};
    }
    int mn = 1e18,pos = 1e18;
    string ans;
    for(auto x : mp){
        if(x.second.first < mn) mn = x.second.first,ans = x.first,pos = x.second.second;
        else if(x.second.first == mn && x.second.second < pos) mn = x.second.first,ans = x.first,pos = x.second.second;
    }
    cout << ans << endl;
    cout << mn + 1000 << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1;
    while(_ --){
        solve();
    }
    return 0;
}

MC0204

求最长连续不重复子序列长度

双指针即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 1e6 + 7;
int n;
int c[N];

void solve(){
    cin >> n;
    for(int i = 0;i < n;i ++) cin >> c[i];
    map<int,int> mp;
    int mx = 1;
    for(int i = 0,j = 0;i < n;i ++){
        mp[c[i]] ++;
        while(j < i && mp[c[i]] > 1) mp[c[j ++]] --;
        mx = max(i - j + 1,mx);
    }
    cout << mx << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1;
    while(_ --){
        solve();
    }
    return 0;
}

MC0205

找最大字符

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    string s; cin >> s;
    char ans = *max_element(s.begin(),s.end());
    cout << ans << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1;
    while(_ --){
        solve();
    }
    return 0;
}

MC0206

只会有两种情况:

ABABAB...

BABABA...

看两种情况最低处理操作即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    string s; cin >> s;
    int n = s.size();
    int ans1 = 0,ans2 = 0;
    for(int i = 0;i < n;i ++){
        if(i % 2){
            if(s[i] == 'A') ans1 ++;
            else ans2 ++;
        } else{
            if(s[i] == 'B') ans1 ++;
            else ans2 ++;
        }
    }
    cout << min(ans1,ans2) << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1; cin >> _;
    while(_ --){
        solve();
    }
    return 0;
}

MC0207

单向建图 dfs从1开始跑一遍看是否能到n即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 2e5 + 7;
vector<int> g[N];
int n,m;
int flag;
int st[N];

void dfs(int u){
    if(flag || u == n){
        flag = 1;
        return;
    }
    st[u] = 1;
    for(auto t : g[u]){
        if(st[t]) continue;
        dfs(t);
    }
}

void solve(){
    cin >> n >> m;
    for(int i = 0;i < m;i ++){
        int u,v; cin >> u >> v;
        g[u].push_back(v);
    }
    dfs(1);
    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1; 
    while(_ --){
        solve();
    }
    return 0;
}

MC0208

看是否是10的倍数 不是就*2

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

int n;

void solve(){
    cin >> n;
    if(n % 10) cout << 2 * n << endl;
    else cout << 0 << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1; 
    while(_ --){
        solve();
    }
    return 0;
}

MC0209

分长度奇偶即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

int n;

void solve(){
    string s; cin >> s;
    n = s.size();
    if(n & 1) cout << "123456" << endl;
    else{
        reverse(s.begin() + n / 2,s.end());
        cout << s << endl;
    }
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1; 
    while(_ --){
        solve();
    }
    return 0;
}

MC0210

暴力判断即可

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

int n;

void solve(){
    string s; cin >> s;
    n = s.size();
    for(int i = n / 3;i >= 1;i --){
        string s1 = s.substr(0,i);
        string s2 = s.substr(n - i,i);
        if(s1 == s2){
            string s3 = s.substr(i,n - 2 * i);
            if(s3.find(s1) != s3.npos){
                cout << s1 << endl;
                return;
            }
        }
    }
    cout << "No" << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1; 
    while(_ --){
        solve();
    }
    return 0;
}

MC0211

模拟

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    string s; cin >> s;
    int pos = s.size() - 1;
    for(int i = pos;i >= 0;i --){
        if(s[i] != '0'){
            pos = i;
            break;
        }
    }
    if(s.size() == 1) cout << s << endl;
    else{
        cout << s[0];
        if(pos != 0) cout << ".";
        for(int i = 1;i <= pos;i ++) cout << s[i];
        cout << "E" << s.size() - 1 << endl;
    }
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1; 
    while(_ --){
        solve();
    }
    return 0;
}

MC0212

code:

cpp 复制代码

MC0213

按相同边长分类讨论

code:

cpp 复制代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

void solve(){
    map<int,int> mp;
    for(int i = 0;i < 5;i ++){
        int x; cin >> x;
        mp[x] ++;
    }
    if(mp.size() == 1) cout << "Square" << endl;
    else if(mp.size() == 2){
        if(mp.begin()->second == 4 || mp.begin()->second == 1) cout << "Square" << endl;
        else cout << "Rectangle" << endl;
    } else if(mp.size() == 3){
        int cnt = 0;
        for(auto x : mp) if(x.second == 2) cnt ++;
        if(cnt == 2) cout << "Rectangle" << endl;
        else cout << "No" << endl;
    }
    else cout << "No" << endl;
} 

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int _ = 1; 
    while(_ --){
        solve();
    }
    return 0;
}
相关推荐
杰九9 分钟前
【算法题】46. 全排列-力扣(LeetCode)
算法·leetcode·深度优先·剪枝
manba_17 分钟前
leetcode-560. 和为 K 的子数组
数据结构·算法·leetcode
liuyang-neu18 分钟前
力扣 11.盛最多水的容器
算法·leetcode·职场和发展
忍界英雄26 分钟前
LeetCode:2398. 预算内的最多机器人数目 双指针+单调队列,时间复杂度O(n)
算法·leetcode·机器人
Kenneth風车27 分钟前
【机器学习(五)】分类和回归任务-AdaBoost算法-Sentosa_DSML社区版
人工智能·算法·低代码·机器学习·数据分析
C7211BA1 小时前
使用knn算法对iris数据集进行分类
算法·分类·数据挖掘
Tisfy1 小时前
LeetCode 2398.预算内的最多机器人数目:滑动窗口+单调队列——思路清晰的一篇题解
算法·leetcode·机器人·题解·滑动窗口
程序猿练习生1 小时前
C++速通LeetCode简单第18题-杨辉三角(全网唯一递归法)
c++·算法·leetcode
Huazzi.1 小时前
算法题解:斐波那契数列(C语言)
c语言·开发语言·算法
汉字萌萌哒1 小时前
【2022 CCF 非专业级别软件能力认证第一轮(CSP-J1)入门级 C++语言试题及解析】
数据结构·c++·算法