RE 3221225725

B3624 猫粮规划

Cute_QiQi @ 2024-04-27 19:27:48

#include<bits/stdc++.h>
using namespace std;
int a[43],n,l,r,ans;
void Dfs(int p,int sign){
    if(sign>r)return;
    if(sign>=l&&sign<=r)ans++;
    if(p==n){
        if(sign>=l&&sign<=r)ans++;
    }
    Dfs(p+1,sign+a[p]);
    Dfs(p+1,sign);
}
int main(){
    cin>>n>>l>>r;
    for(int i=0;i<n;++i)cin>>a[i];
    Dfs(0,0);
    cout<<ans<<'\n';
}

帮帮忙QwQ


by Jason_Ming @ 2024-04-27 19:32:39

无穷递归导致栈溢出了

#include<bits/stdc++.h>
using namespace std;
int a[43],n,l,r,ans;
void Dfs(int p,int sign){
    if(sign>r)return;
    if(sign>=l&&sign<=r)ans++;
    if(p==n){
        return;
    }
    Dfs(p+1,sign+a[p]);
    Dfs(p+1,sign);
}
int main(){
    cin>>n>>l>>r;
    for(int i=0;i<n;++i)cin>>a[i];
    Dfs(0,0);
    cout<<ans<<'\n';
}

by Jason_Ming @ 2024-04-27 19:33:20

@SixSeven


by Cute_QiQi @ 2024-04-27 19:40:33

@Jason_Ming 0pts,不过谢谢你


|