【CCCC】2024团体程序设计天梯赛补题

文章展示了编程解决实际问题的多个实例,包括基础算术操作、颜色识别、数据统计、字符串处理、图论应用等,以及一些特定场景下的算法实现如九宫格和夺宝大赛。
摘要由CSDN通过智能技术生成

L1-1 编程解决一切

#include <bits/stdc++.h>

using namespace std;

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

L2-2 再进去几个人

#include <bits/stdc++.h>

using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << b - a << endl;
}

L1-3 帮助色盲

#include <bits/stdc++.h>

using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) cout << "biii\nstop";
    else if (a == 0 && b == 1) cout << "-\nstop";
    else if (a == 1 && b == 0) cout << "dudu\nmove";
    else if (a == 1 && b == 1) cout << "-\nmove";
    else if (a == 2) cout << "-\nstop";
}

L1-4 四项全能

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int res = n;
    for (int i = 1; i <= m ;i++) {
        int x;
        cin >> x; 
        res -= (n - x);
    }
    // 20 + 15 + 8 + 4 = 47 剩下3人全能 
    cout << max(0, res);
}

L1-5 兰州牛肉面

#include <bits/stdc++.h>

using namespace std;

const int N = 1e2 + 7;

int n, x, y;
double a[N], sum;
int cnt[N];

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    while (cin >> x >> y && x && y) {
        cnt[x] += y;
        sum += y * a[x] * 1.0;
    }
    for (int i = 1; i <= n; i++) {
        cout << cnt[i] << endl;
    }
    cout << fixed << setprecision(2) << sum << endl;
}

L1-6 别再来这么多猫娘了

思路:枚举截取片段比较,但违禁词要按照输入顺序依次处理(未处理,14分)

#include <bits/stdc++.h>

using namespace std;

int n, m, k, cnt, mx = -1;
string str, res;
map<string, int> mp;

int main() {
    cin >> n; cin.get();
    for (int i = 1; i <= n; i++) {
        getline(cin, str);
        mp[str] = 1, mx = max(mx, (int)str.size());
    }
    cin >> k; cin.get();
    getline(cin, str);
    for (int i = 0; i < (int)str.size(); i++) {
        bool flag = false;
        for (int j = 1; j <= mx; j++) {
            string cur = str.substr(i, j);
            if (mp.count(cur)) {
                flag = true;
                res += "<censored>";
                i += j - 1, cnt ++;
                break;
            }
        }
        if (!flag) res.push_back(str[i]);
    }
    if (cnt >= k) cout << cnt << endl << "He Xie Ni Quan Jia!" << endl;
    else cout << res << endl;
    return 0;
}

正解 : find,replace处理字符串 同时注意不能直接替换,否则会超时

#include <bits/stdc++.h>

using namespace std;

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

int main() {
    cin >> n; cin.get();
    for (int i = 1; i <= n; i++) {
        getline(cin, str);
        limits.push_back(str);
    }
    cin >> k; cin.get();
    getline(cin, str);
    //1.禁词可能是“<censored>”的子串 2.违禁词可能是另一个违禁词的子串
    for (auto c : limits) {
       while (str.find(c) != string::npos) {
           str.replace(str.find(c), c.length(), "-");
           cnt ++;
       }
    }
    while (str.find("-") != string::npos) {
        str.replace(str.find("-"), 1, "<censored>");
    }
    if (cnt >= k) cout << cnt << "\nHe Xie Ni Quan Jia!\n";
    else cout << str << endl;
    return 0;
}

L1-7 整数的持续性

#include <bits/stdc++.h>

using namespace std;;

int getLen(int x) {
    int res = 0;
    while (x) res ++, x /= 10;
    return res;
}

int getNext(int x) {
    int res = 1;
    while (x) res *= x % 10, x /= 10;
    return res;
}

int getCnt(int x) {
    int res = 0;
    while (x && getLen(x) != 1) {
        x = getNext(x);
        res ++;
    }
    return res;
}

int main() {
    int a, b;
    cin >> a >> b;
    unordered_map<int, int> mp;
    int maxv = -1;
    for (int i = a; i <= b; i++) {
        maxv = max(maxv, getCnt(i));
    }
    bool flag = true;
    cout << maxv << endl;
    for (int i = a; i <= b; i++) {
        if (maxv == getCnt(i))
            if (flag) cout << i, flag = false;
            else cout << ' ' << i;
    }
}

L1-8 九宫格

思路:按题意枚举行列3*3格子的合法性

#include <bits/stdc++.h>

using namespace std;;

const int N = 15;

int n, a[N][N];

bool check(int x, int y) {
    unordered_map<int, int> mp;
    for (int i = x; i < x + 3; i++)
        for (int j = y; j < y + 3; j++)
            mp[a[i][j]] ++;
    for (int i = 1; i <= 9; i++) 
        if (!mp.count(i) || mp[i] >= 2)
            return false;
    return true;
}

int main() {
    cin >> n;
    while (n --) {
        bool row = true, col = true;
        for (int i = 1; i <= 9; i++)
            for (int j = 1; j <= 9; j++) 
                cin >> a[i][j];

        for (int i = 1; i <= 9; i++) {
            unordered_map<int, int> mp;
            for (int j = 1; j <= 9; j++) mp[a[i][j]] ++;
            for (int j = 1; j <= 9; j++)
                if (!mp.count(j) || mp[j] >= 2)
                    row = false;
        }

        for (int i = 1; i <= 9; i++) {
            unordered_map<int, int> mp;
            for (int j = 1; j <= 9; j++) mp[a[j][i]] ++;
            for (int j = 1; j <= 9; j++)
                if (!mp.count(j) || mp[j] >= 2)
                    col = false;
        }
        if (row && col && check(1, 1) && check(1, 4) && check(1, 7)
           && check(4, 1) && check(4, 4) && check(4, 7)
           && check(7, 1) && check(7, 4) && check(7, 7))
            cout << 1 << endl;
        else cout << 0 << endl;
    }
}

L2-1鱼与熊掌

思路:按题意枚举即可,不会超时

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 7;

int n, m, k, x, a, b;
vector<int> g[N];

int main() {
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> k;
        for (int j = 1; j <= k; j++) {
            cin >> x;
            g[i].push_back(x);
        }
    }
    cin >> k;
    while (k --) {
        cin >> a >> b;
        int res = 0;
        for (int i = 1; i <= n; i++) {
            int cnt = 0;
            for (auto c : g[i])
                if (c == a || c == b) 
                    cnt ++;
            if (cnt == 2) res ++;
        }
        cout << res << endl;
    }
}

L2-2 懂蛇语

思路:字符串处理,map,string, stringstream的使用

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 7;

int n, m, k, x, a, b;
string str, s, word;
map<string, vector<string>> mp;

int main() {
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin >> n;
    cin.get();
    while (n --) {
        getline(cin, str);
        stringstream ss(str);
        string cur;
        while (ss >> word) cur.push_back(word[0]);
        mp[cur].push_back(str);
    }
    cin >> m;
    cin.get();
    while (m --) {
        getline(cin, str);
        stringstream ss(str);
        string cur;
        while (ss >> word) cur.push_back(word[0]);
        if (mp.count(cur)) {
            auto res = mp[cur];
            sort(res.begin(), res.end());
            for (int i = 0; i < (int)res.size(); i++) {
                if (i) cout << '|' << res[i];
                else cout << res[i];
            }
            cout << endl;
        } else {
            cout << str << endl;
        }
    }
}

L2-3 满树的遍历

思路:按题意找根节点,统计每个结点的出度,非叶节点的最大度数即位树阶数,若非叶节点的度数都为同一个则为k阶满树。

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 7;

int n, m, k, x, a, b;
int f[N], dout[N];
bool flag;
vector<int> g[N];

void dfs(int u) {
    if (!flag) flag = true, cout << u;
    else cout << ' ' << u;
    for (auto c : g[u])
        dfs(c);
}

signed main() {
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin >> n;
    int root = 0, maxv = -1;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        f[i] = x, dout[x] ++;
        if (x == 0) root = i;
        g[x].push_back(i);
    }
    set<int, greater<int>> s;
    for (int i = 1; i <= n; i++) {
        maxv = max(maxv, dout[i]);
        sort(g[i].begin(), g[i].end());
        if (dout[i])
            s.insert(dout[i]);
    }

    if (maxv == 0) { //特判
        cout << 0 << ' ' << "yes\n";
        cout << 1 << endl;
        return 0;
    }

    cout << *s.begin() << (s.size() == 1 ? " yes\n" : " no\n");
    dfs(root);
    return 0;
}

L2-4 吉利矩阵(待补)

L3-1 夺宝大赛

思路:BFS搜索每个起点的最短路存起来,再把被消掉的idx标记,最后未被消掉的起点中时间最短的即为答案 (18分超时)

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;
const int N = 1e2 + 7, M = 5e3 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int n, m, k, x, y;
int g[N][N];
bool st[N][N], vis[M];

struct Node {
    int x, y, step;
};

int bfs(int sx, int sy) {
    queue<Node> q;
    memset(st, false, sizeof st);
    q.push({sx, sy, 0});
    st[sx][sy] = true;
    while (!q.empty()) {
        auto t = q.front();
        q.pop();
        if (g[t.x][t.y] == 2) return t.step;
        for (int i = 0; i < 4; i++) {
            int a = t.x + dx[i], b = t.y + dy[i];
            if (a < 1 || a > n || b < 1 || b > m || !g[a][b]) continue;
            if (st[a][b]) continue;
            if (g[a][b] == 2) return t.step + 1;
            q.push({a, b, t.step + 1});
            st[a][b] = true;
        }
    }
    return -1;
}

int main() {
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++)
            cin >> g[i][j];

    cin >> k;
    map<int, int> mp, cnts;
    map<int, vector<int>> ump;
    set<int> steps;
    for (int i = 1; i <= k; i++) {
        cin >> y >> x;
        int step = bfs(x, y);
        cnts[i] = step;
        if (step != -1) {
            mp[step] ++, ump[step].push_back(i);
            steps.insert(step);
        }
    }
    //for (auto c : steps) cout << c << ' '; cout << endl;
    for (auto c : steps) {
        if (mp[c] >= 2)  
            for (auto idx : ump[c])
                vis[idx] = true; //标记被消掉的
    }
    vector<PII> res;
    for (int i = 1; i <= k; i++) {
        if (!vis[i] && cnts[i] != -1) 
            res.push_back({i, cnts[i]});
    }
    sort(res.begin(), res.end(), [&] (auto a, auto b) {
        return a.second < b.second;
    });
    
    if (res.empty()) {
        cout << "No winner.";
        return 0;
    }
    
    cout << res[0].first << ' ' << res[0].second << endl;
}

正解:从终点跑最短路到起点即可

#include <bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;
const int N = 1e2 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int n, m, k, x, y, sx, sy;
int g[N][N];
int dist[N][N];

void bfs() {
    queue<PII> q;
    q.push({sx, sy});
    memset(dist, -1, sizeof dist);
    dist[sx][sy] = 0;
    while (!q.empty()) {
        auto t = q.front(); q.pop();
        for (int i = 0; i < 4; i++) {
            int a = t.x + dx[i], b = t.y + dy[i];
            if (a < 1 || a > n || b < 1 || b > m) continue;
            if (dist[a][b] != -1 || !g[a][b]) continue;
            q.push({a, b});
            dist[a][b] = dist[t.x][t.y] + 1;
        }
    }
}

int main() {
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
    	for (int j = 1; j <= m; j++) {
            cin >> g[i][j];
            if (g[i][j] == 2) sx = i, sy = j;
        }
	}
	
	bfs();
	
    cin >> k;
    map<int, vector<int>> steps;
    for (int i = 1; i <= k; i++) {
        cin >> y >> x;
        if (dist[x][y] != -1)
        	steps[dist[x][y]].push_back(i);
    }
    
    int idx = 0, res = 0; 
    for (auto [k, v] : steps) {
   	    if (v.size() == 1 ) {
       		res = k, idx = v[0];
       		break;
		}
	}
	if (!res) cout << "No winner.\n";
	else cout << idx << ' ' << res << endl;
    return 0;
}
  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值