样例过了,但是还是不正确,求求大佬帮帮忙

B3624 猫粮规划

ZASG @ 2024-08-13 20:34:44

#include <iostream>
using namespace std;

const int N = 10010;

int res = 0;

int n,l,r;

int w[N];

int path[N];

void dfs(int u,int start, int sum){
    if(sum >= l && sum <= r) {
        res++;
        return;
    }
    if(sum > r) return;

    for(int i = start; i < n; i++){
        path[u] = w[i];
        dfs(u+1,i+1,sum + w[i]);
        path[u] = 0;
    }

}

int main(){
    cin >> n >> l >> r;
    for(int i = 0; i < n; i++){
        cin >> w[i];
    }
    dfs(0,0,0);
    cout << res;

    return 0;
}

by Shi_114514 @ 2024-08-13 20:49:04

为什么我发布不了啊!


by Shi_114514 @ 2024-08-13 20:54:24

回复失败 拒绝: 未按要求排版(未正确标记代码部分

这怎么回事啊!!!


by Shi_114514 @ 2024-08-13 21:06:16

@ZASG 私信你了


by ZASG @ 2024-08-13 21:26:18

#include <iostream>
using namespace std;

const int N = 10010;

int res = 0;

int n,l,r;

int w[N];

int path[N];

void dfs(int u,int start, int sum){
    if(sum > r) return;//退出条件是超过右边界
    if(sum >= l) {
        for(int i = 0; i < u; i++){
            printf("%d ",path[i]);
        }
        cout << '\n';
            res++;
    }

    for(int i = start; i < n; i++){
        path[u] = i;//路劲追踪,可以不要
        dfs(u+1,i+1,sum + w[i]);
        path[u] = 0;//回溯
    }

}

int main(){
    cin >> n >> l >> r;
    for(int i = 0; i < n; i++){
        cin >> w[i];
    }
    dfs(0,0,0);
    cout << res;

    return 0;
}

by ZASG @ 2024-08-13 21:27:53

@Shi_114514 感谢大哥,我自己调试出来了,是递归的退出条件有问题


by Shi_114514 @ 2024-08-13 22:14:31

@ZASG 嗯


|