有点不能理解,为啥一个WA,一个AC

B3624 猫粮规划

最萌軒宇 @ 2022-10-23 19:04:40

AC:

#include <iostream>
using namespace std;
int n, l, r, ans=0;
int a[55], w[55];
void dfs(int pos) {
    int sum = 0;
    for(int i = 1; i <= pos - 1; i++) {
        if(a[i]!=0)
            sum+=a[i] * w[i];
    }
    if(sum>r) return;
    if(pos == n + 1) {
        if(sum<=r && sum>=l)
            ans++;
        return;
    }
    for(int i = 0; i <= 1; i++) {
        a[pos]=i;
        dfs(pos+1);
    }
}
int main() {
    cin >> n >> l >> r;
    for(int i = 1; i <= n; i++)
        cin >> w[i];
    dfs(1);
    cout << ans << endl;
    return 0;
}

WA:

#include <cstdio>
using namespace std;
int n, l, r, ans = 0;
int a[55], w[55];
void dfs(int pos) {
    int sum = 0;
    for(int i = 1; i <= pos - 1; i++) {
        if(a[i] != 0) {
            sum +=a[i] * w[i];
        }
    }
    if(sum > r) {
        return;
    }
    if(pos == n + 1) {
        if(sum <= r && sum >= 1) {
            ans++;
        }
        return;
    }
    for(int i = 0; i <= 1; i++) {
        a[pos] = i;
        dfs(pos + 1);
    }
}
int main() {
    scanf("%d%d%d", &n, &l, &r);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &w[i]);
    }
    dfs(1);
    printf("%d\n", ans);
    return 0;
}

by AirQwQ @ 2022-10-23 19:16:30

if(pos == n + 1) {
    if(sum<=r && sum>=l)
        ans++;
    return;
}
if(pos == n + 1) {
    if(sum <= r && sum >= 1) {
        ans++;
    }
    return;
}

sum>=1 还是 sum>=l


by AirQwQ @ 2022-10-23 19:16:46

@最萌軒宇


by 最萌軒宇 @ 2022-10-23 19:18:14

@Air_zyc 谢谢了,我真的会xie,这玩意找半天都不知道哪WA了,谢谢


|