洛谷训练新手村之“BOSS战入门综合练习1”题解

P1478 陶陶摘苹果(升级版)

题目链接:https://www.luogu.com.cn/problem/P1478
题目大意:陶陶有s点体力值,每个苹果消耗体力值,问s体力值最多能摘多少苹果。
解题思路:首先过滤掉摘不到的苹果。其次从小到大排序, 贪心 思想:优先选择消耗体力值小的苹果。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5050;
int n, m, s, a, b, x[maxn], y[maxn], ans;
int main() {
    cin >> n >> s >> a >> b;
    a += b;
    while (n --) {
        cin >> x[m] >> y[m];
        if (x[m] <= a) m ++;
    }
    sort(y, y+m);
    for (int i = 0; i < m; i ++) {
        if (y[i] <= s) {
            s -= y[i];
            ans ++;
        }
        else break;
    }
    cout << ans << endl;
    return 0;
}

P1618 三连击(升级版)

题目链接:https://www.luogu.com.cn/problem/P1618
题目大意:求出所有壁纸为A:B:C的三位数。
解题思路:遍历第一个数,判断后两个数是否都是三位数且刚好这三个数从1到9。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int A, B, C;
bool t[10];
bool check(int a) {
    if (a % A) return false;
    int b = a/A*B, c = a/A*C;
    if (b >= 1000 || c >= 1000) return false;
    int d = a * 1000000 + b * 1000 + c;
    memset(t, 0, sizeof(t));
    while (d) {
        t[d%10] = true;
        d /= 10;
    }
    for (int i = 1; i < 10; i ++) if (!t[i]) return false;
    return true;
}
int main() {
    cin >> A >> B >> C;
    bool flag = false;
    for (int a = 123; a < 333; a ++) {
        if (check(a)) {
            flag = true;
            cout << a << " " << a/A*B << " " << a/A*C << endl;
        }
    }
    if (!flag) puts("No!!!");
    return 0;
}

P1579 哥德巴赫猜想(升级版)

题目链接:https://www.luogu.com.cn/problem/P1579
题目大意:给你一个数,找三个素数使得它们的和是这个数。
解题思路:数据量偏大,所以采用素数筛法,然后再枚举。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 20020;
bool pp[maxn];
int n, a, p[maxn];
void init() {
    pp[0] = pp[1] = true;
    for (int i = 2; i < maxn; i ++)
        if (!pp[i]) {
            p[n++] = i;
            for (int j = i; j < maxn/i; j ++) pp[i*j] = true;
        }
}
int main() {
    init();
    cin >> a;
    for (int i = 0; i < n; i ++)
        for (int j = i; j < n; j ++) {
            if (p[i] + p[j] >= a) continue;
            if (!pp[a- p[i] - p[j] ]) {
                cout << p[i] << " " << p[j] << " " << a-p[i]-p[j] << endl;
                return 0;
            }
        }
    return 0;
}

P2089 烤鸡

题目链接:
题目大意:
猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10种配料(芥末、孜然等),每种配料可以放1—3克,任意烤鸡的美味程度为所有配料质量之和
现在,Hanke想要知道,如果给你一个美味程度,请输出这10种配料的所有搭配方案

解题思路:枚举出所有的状态方案。因为直接枚举得开好多循环,所以我可以采用类似 状态压缩 的思想,每个状态对应一个三进制数,然后三进制数的每一位对应10个数字,这样比较好解决一些。
实现代码如下:

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

int a[10], n, res;

int main() {
    cin >> n;
    if (n < 10 || n > 30) {
        puts("0");
        return 0;
    }
    n -= 10;
    for (int i = 0; i < 59049; i ++) {
        int tmp = i, cnt = 0;
        for (int j = 9; j >= 0; j --) {
            a[j] = tmp % 3;
            tmp /= 3;
            cnt += a[j];
        }
        if (cnt == n) res ++;
    }
    cout << res << endl;
    for (int i = 0; i < 59049; i ++) {
        int tmp = i, cnt = 0;
        for (int j = 9; j >= 0; j --) {
            a[j] = tmp % 3;
            tmp /= 3;
            cnt += a[j];
        }
        if (cnt != n) continue;
        for (int j = 0; j < 10; j ++) {
            if (j) putchar(' ');
            cout << a[j]+1;
        }
        cout << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/quanjun/p/11927193.html