第十次训练课题解

· · 个人记录

B2110 找第一个只出现一次的字符

问题描述

给定一个字符串 s,找出第一个只出现一次的字符。如果没有这样的字符,输出 "no"。

解决方案

  1. 统计每个字符出现的次数:使用一个大小为26的数组 cnt 来记录每个字符(假设都是小写字母)出现的次数。
  2. 从前往后找到第一个出现一次的字符:再次遍历字符串,找到第一个在 cnt 数组中计数为1的字符。

C++代码

#include<bits/stdc++.h>
using namespace std;

int cnt[26];
string s;

int main() {
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
        int v = s[i] - 'a';
        cnt[v]++;
    }

    for (int i = 0; i < s.size(); i++) {
        int v = s[i] - 'a';
        if (cnt[v] == 1) {
            cout << s[i];
            return 0;
        }
    }

    cout << "no";
    return 0;
}


P1887 乘积最大3

问题描述

给定两个整数 n 和 m,找出 m 个数字使得它们的和为 n,并且这 m 个数字的乘积最大。

解决方案

  1. 将 n 平均分配到 m 个数字:每个数字尽量相等时,乘积最大。
  2. 处理余数:如果 n \mod m \neq 0,则将余数均匀分配到前几个数字上。

C++代码

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int base = n / m;
    int extra = n % m;

    for (int i = 1; i <= m - extra; i++) {
        cout << base << ' ';
    }

    for (int i = 1; i <= extra; i++) {
        cout << base + 1 << ' ';
    }

    return 0;
}


P1075 [NOIP 2012 普及组] 质因数分解

问题描述

给定一个整数 n,将其分解为两个不同的质数的乘积。

解决方案

  1. 枚举较小的因子:从2开始枚举到 \sqrt{n},检查是否存在一个因子。
  2. 计算另一个因子:如果找到了一个因子 i,则另一个因子为 n / i。

C++代码

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            cout << n / i;
            return 0;
        }
    }
    return 0;
}


P1293 班级聚会

问题描述

给定多个城市及其离莫斯科的距离,以及每个城市的人数,选择一个城市作为聚会地点,使总花费最小。如果有多个城市花费相同,则选择离莫斯科最近的城市。

解决方案

  1. 读取输入数据:存储每个城市的距离、人数和名称。
  2. 排序:按离莫斯科的距离从小到大排序。
  3. 枚举每个城市作为聚会地点:计算总花费,找到最小值。

C++代码

#include<bits/stdc++.h>
using namespace std;

const int N = 1005;

struct node {
    int cnt, dist;
    string name;
} a[N];

bool cmp(node x, node y) {
    return x.dist < y.dist;
}

int main() {
    int n = 0;
    while (true) {
        n++;
        cin >> a[n].cnt >> a[n].dist >> a[n].name;
        if (a[n].name == "Moscow") break;
    }

    sort(a + 1, a + 1 + n, cmp);

    int minn = 1e9;
    string ans;

    for (int i = 1; i <= n; i++) {
        int sum = 0;
        for (int j = 1; j <= n; j++) {
            sum += a[j].cnt * abs(a[i].dist - a[j].dist);
        }
        if (sum < minn) {
            minn = sum;
            ans = a[i].name;
        }
    }

    cout << ans;
    return 0;
}


B3754 [信息与未来 2019] 鸡兔同笼

问题描述

给定 x 个头和 y 只脚,求最多和最少有多少只兔子。

解决方案

  1. 最多兔子:
    • 如果全部是兔子,需要 4x 只脚。
    • 如果脚的数量少于 4x,则需要将一些兔子换成鸡或三脚猫。
  2. 最少兔子:
    • 如果全部是三脚猫,需要 3x 只脚。
    • 如果脚的数量多于 3x,则需要至少一只兔子。

C++代码

#include<bits/stdc++.h>
using namespace std;

int main() {
    int T;
    cin >> T;
    while (T--) {
        int x, y;
        cin >> x >> y;

        // 最少兔子
        if (3 * x >= y) {
            cout << 0 << ' ';
        } else {
            cout << y - 3 * x << ' ';
        }

        // 最多兔子
        int max_rabbits = x - (4 * x - y + 1) / 2;
        cout << max_rabbits << endl;
    }
    return 0;
}

B3691 [语言月赛202212] 狠狠地切割(Easy Version)

问题描述

给定两个数组 a 和 b,找出 a 中哪些元素是 b 的元素,并据此切割 a 成若干段。

解决方案

  1. 排序数组 b:便于二分查找。
  2. 遍历数组 a:对于每个元素,使用二分查找判断是否在 b 中。
  3. 统计切割段数:每当遇到一个不在 b 中的元素时,增加一段。

C++代码

#include<bits/stdc++.h>
using namespace std;

const int N = 100005;

int a[N], b[N];
int n, m;

bool binary_search(int x) {
    int l = 0, r = m + 1;
    while (l + 1 < r) {
        int mid = (l + r) / 2;
        if (b[mid] < x) {
            l = mid;
        } else {
            r = mid;
        }
    }
    return r != m + 1 && b[r] == x;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= m; i++) {
        cin >> b[i];
    }

    sort(b + 1, b + 1 + m);

    int ans = 0, cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (binary_search(a[i])) {
            if (cnt != 0) {
                ans++;
            }
            cnt = 0;
        } else {
            cnt++;
        }
    }

    if (cnt != 0) {
        ans++;
    }

    cout << ans;
    return 0;
}