0
点赞
收藏
分享

微信扫一扫

2019年广东工业大学腾讯杯新生程序设计竞赛(同步赛)

践行数据分析 2023-02-15 阅读 69

同步赛链接

A-原初的信纸(最值,STL)

题意:

参考代码:

void solve() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (auto &c : a)
        std::cin >> c;
    std::cout << *max_element(a.begin(), a.end()) << '\n';
}

B-骑士的对决(思维)

思路:

参考代码:

#include <bits/stdc++.h>

//模拟

using ll = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    char c;
    std::string s;
    std::cin >> s >> c;
    std::map<char, bool> mp;
    mp[s[0]] = true, mp[s[1]] = true;
    if (mp['S'] && mp['J'])
    {
        if (c == 'S') {
            std::cout << "lyrnb";
        } else {
            std::cout << "pmznb";
        } 
    } else if (mp['S'] && mp['B']) {
        if (c == 'B') {
            std::cout << "lyrnb";
        } else {
            std::cout << "pmznb";
        }
    } else {
        if (c == 'J') {
            std::cout << "lyrnb";
        } else {
            std::cout << "pmznb";
        }  
    }
    return 0;
}

C-秘密的议会(算数)

参考代码:

void solve() {
    int cnty{}, cntn{};
    std::string s;
    std::cin >> s;
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
    for (char c : s) {
        cnty += (c == 'y');
        cntn += (c == 'n');
    }
    int len = s.size();
    if (cnty >= len / 2) {
        std::cout << "pmznb\n";
    } else if (cntn >= len / 2) {
        std::cout << "lyrnb\n";
    } else {
        std::cout << "wsdd\n";
    }
}

D-城市的税金(模拟)

参考代码:

void solve() {

    int n, m;
    std::cin >> n >> m;
    std::vector<int> a(n + 1);

    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
    }

    int op, l, r;
    while (m--) {
        std::cin >> op >> l >> r;
        if (op == 1) {
            for (int i = l; i <= r; i++)
                a[i] = 1ll * a[i] * 251 % 996 * 404 * 123;
        } else {
            std::map<ll, ll> mp;
            ll max = -1;
            for (int i = l; i <= r; i++) {
                mp[a[i]]++;
            }
            for (auto it : mp) {
                max = std::max(max, it.second);
            }
            std::cout << max << '\n';
        }
    }
}

E-缺席的神官(贪心)

思路:

参考代码:

void solve() {
    int n, m, k;
    std::cin >> n >> m >> k;
    std::vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++)
        std::cin >> a[i];
    for (int i = 1; i < n; i++)
        b[i] = a[i + 1] - a[i];
    std::sort(b.begin() + 1, b.end() - 1);
    ll sum = std::accumulate(b.begin() + 1, b.begin() + n + 1 - k, 0LL);
    std::cout << sum + k;
}

F-失踪的玫瑰(思维)

思路:

参考代码:

void solve() {
    int n;
    std::cin >> n;
    if (n & 1) {
        for (int i = 2; i < n; i++)
            std::cout << i << " ";
        for (int i = 2; i < n; i++)
            std::cout << i << ' ';
    } else {
        for (int i = 2; i < n; i++)
            std::cout << i << " ";
        for (int i = n - 1; i >= 2; i--)
            std::cout << i << ' ';
    }
    std::cout << '\n';
}

G-虚数的纸牌(模拟)

参考代码:

int change(char s) {
    if (s >= '3' && s <= '9')
        return s - '0';
    if (s == '0')
        return 10;
    if (s == 'J')
        return 11;
    if (s == 'Q')
        return 12;
    if (s == 'K')
        return 13;
    if (s == 'A')
        return 14;
    if (s == '2')
        return 15;
}

void solve() {
    std::string s;
    std::cin >> s;
    int res = s.size(); // 单
    int cnt[16] = {0};
    for (char c:s) {
        cnt[change(c)]++; // 出现次数
    }
    for (int i = 3; i <= 15; i++) {
        if (cnt[i] >= 2)
            res += 1ll * cnt[i] * (cnt[i] - 1) / 2; // 对子
        if (cnt[i] == 4) {
            res++;                     // 炸弹
            res += 4 * (s.size() - 4); // 三带一,减去自己的4张
            for (int j = 3; j <= 15; j++) {
                if (i != j) {
                    res += 4ll * cnt[j] * (cnt[j] - 1) / 2; // 三带二
                }
            }  
        }
        else if (cnt[i] == 3) {
            res += s.size() - 3; // 三带一
            for (int j = 3; j <= 15; j++) {
                if (i != j) {
                    res += 1ll * cnt[j] * (cnt[j] - 1) / 2; // 三带二
                }
            }
                
        }
        if (i <= 11) {
            int t = 1;
            for (int j = 0; j < 5; ++j) // 五单顺子
                t *= cnt[i + j];
            res += t;
        }
    }
    std::cout << res << '\n';
}

H-绵羊的银币(思维)

思路:

参考代码:

#include <bits/stdc++.h>

using ll = long long;

ll f[99];

void solve() {
    ll p = 1, x = 0, n;
    std::cin >> n;
    while (p <= n)
        p <<= 1, x++;
    std::cout << f[x - 1] << '\n';
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    f[0] = f[1] = 1;
    for (int i = 2; i <= 90; i++)  //初始化
        f[i] = f[i - 1] + f[i - 2];
    while (t--) {
        solve();
    }
    return 0;
}

I-迷途的怪物(思维)

参考代码:

void solve() {
    std::string s;
    int p;
    std::cin >> p >> s;
	if ((s.back()-'0') & 1) {
        std::cout << p - 1 << '\n';
    } else {
        std::cout << "1\n";
    }
}

J-简单的数学(数学题)

参考代码:

void solve() {
    int n;
    std::cin >> n;
    if (n & 1) {
        std::cout << 1ll * (n + 1) * n << '\n';
    } else {
        std::cout << 1ll * (n + 1) * n * (-1) << '\n';
    }
}

K-消亡的质数(思维,数学)

思路:

参考代码:

#include <bits/stdc++.h>

using ll = long long;

ll judge(ll x) {
    if (x <= 0)
        return 0;
    for (ll i = 1; i * i + i * (i + 1) + (i + 1) * (i + 1) <= x; i++) {
        if (i * i + i * (i + 1) + (i + 1) * (i + 1) == x) {
            return 1;
        }
    } 
    return 0;
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    while (t--) {
        ll p;
        std::cin >> p;
        std::cout << (judge(p) ? "YES\n" : "NO\n");
    }
    return 0;
}

L-危险的台阶(思维)

我首先想的是从最高(外)处那块石板开始考虑,假设每块石板的长度为1。当只有一块石阶时,显然,由于石板密度均匀,一块长度为1的石板的重心即为1/2,因此,最大伸出长度为1/2。然后,考虑两块石板时的情况,如下图。只需找到两块石板的重心即可,显然可得,最大长度为1/2+1/4=3/4。结果应是 (1/2)L+(1/4)L+(1/6)L+…+(1/2n)L(可根据重心联立方程组解得第三块石板伸出长度为1/6).

参考代码:

#include <bits/stdc++.h>

using ll = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll n, l, m;
    std::cin >> n >> l >> m;
    double sum = 0,s = 2;
    int i = 1;
    while (n--) {
        sum += l / (s * i++);
    }
    std::cout << std::fixed << std::setprecision(4) << sum;
    return 0;
}

M-破碎的愿望(思维,STL)

思路:

参考代码:

#include <bits/stdc++.h>

using ll = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll n, k;
    std::string s;
    std::cin >> n >> k >> s;
    ll x = k % (n * 2);
    std::string s1 = s;
    std::reverse(s1.begin(), s1.end());
    s = s + s1;
    if (x == 0) {
        std::cout << s[n * 2 - 1];
    } else {
        std::cout << s[x - 1];
    }
    return 0;
}
举报

相关推荐

0 条评论