WA求助,悬一关

B3624 猫粮规划

yyb18660335329 @ 2023-10-01 20:13:40

#include<bits/stdc++.h>
using namespace std;
int n,l,r;
int foods[10005];
int tot=0;
void dfs(int di,int alls)
{
    if(alls>r)return;
    if(alls>=l&&alls<=r) tot++;
    if(di>n) return;
    dfs(di+1,alls);
    dfs(di+1,alls+foods[di]);
}
int main()
{
    cin>>n>>l>>r;
    for(int i=1;i<=n;i++)
    {
        cin>>foods[i];
    }
    dfs(1,0);
    cout<<tot;
 } 

rt求助


by onlyfiee @ 2023-10-01 20:43:28

@yyb18660335329 多计数了,


by onlyfiee @ 2023-10-01 20:48:27

比如你一开始选择了正确的方案,但是你后面多计数了

比如输入

3 10 20

5 5 1

答案是 1 ,而你是6


by sandwich03 @ 2023-10-01 20:48:35

void dfs(int x,int y){
    if(y>r) return;
    if(x>n)//判断食物是否取完
    {
        if(y>=l&&y<=r) ans++;
        return;
    }
    dfs(x+1,y+w[x]);
    dfs(x+1,y);
    return;
}

by sandwich03 @ 2023-10-01 20:49:07

改成这样 @yyb18660335329


by onlyfiee @ 2023-10-01 20:49:11

就是后面的不选择也被统计成方案了


by onlyfiee @ 2023-10-01 20:50:45

答案是2


by yyb18660335329 @ 2023-10-02 08:01:57

@linzhi123 @onlyfiee 一关,感谢


by ping_lanjiu @ 2024-03-14 21:27:29

@linzhi123 大佬能详细解释一下为什么只有搜索完的情况是满足的,也是这个错误,没想明白,求教了。


|