求助大佬,样例过了,但全wa了

B3624 猫粮规划

everyday_ac_2 @ 2024-07-24 16:43:44

#include<iostream>
using namespace std;

int n, l, r;
int w[50];
long long plan = 0;

int judge(int x) {
    if (x > r) {
        return 0;
    }
    else if (x < l) {
        return 1;
    }
    else {
        //cout << x << ' ' << plan << '    ';
        plan = plan + 1;
        return 1;
    }
}

void dfs(int s, int sum) {
    if (judge(sum) == 0)return;
    if (s > n)return;
    dfs(s + 1, sum + w[s]);
    dfs(s + 1, sum);
}

int Fread() {
    char x = getchar();
    int a = 0;
    int b = 1;
    while (x < '0' || x>'9') {
        if (x == '-') {
            b = -1;
        }
        x = getchar();
    }
    while (x >= '0' && x <= '9') {
        a = (a * 10) + (x - '0');
        x = getchar();
    }
    return b * a;
}

int Fwrite(int f) {
    if (f == 0) {
        putchar('0');
        return  0;
    }
    if (f < 0) {
        putchar('-');
        f = -f;
    }
    int i = 0;
    char buffer[10000];
    while (f > 0) {
        buffer[i] = f % 10 + '0';
        f = f / 10;
        i++;
    }
    for (int j = i - 1; j >= 0; --j) {
        putchar(buffer[j]);
    }
    return 0;
}

int main() {
    n = Fread();
    l = Fread();
    r = Fread();
    for (int i = 1; i <= n; i++) {
        w[i] = Fread();
    }
    dfs(1, 0);
    Fwrite(plan);
    return 0;
}

by Dangerise @ 2024-07-24 17:04:25

@fryfry

注意,在dfs中,一定要搜完再存储结果

void dfs(int s, int sum) {
    if (sum > r)
        return;
    if (s > n) {
        if (sum >= l && sum <= r) {
            plan++;
        }
        return;
    }
    dfs(s + 1, sum + w[s]);
    dfs(s + 1, sum);
}

by everyday_ac_2 @ 2024-07-24 18:38:19

@Dangerise 谢谢大佬


|