大佬求助
查看原帖
大佬求助
1045470
lichengze123456楼主2024/12/11 14:57

起因是做这道题

然后是时间卡不过

最后是求助大佬

#include<bits/stdc++.h>
using namespace std;
string s;
int n,ans=0x3f3f3f3f;
void dfs(int x,int p,string cnt,int k){
    if(stoi(cnt)+p>n){
        return;
    }
    if(x==s.size()){
        if(p+stoi(cnt)==n){
            ans=min(k,ans);
        }
        return;
    }
    if(p+stoi(cnt)<=n)dfs(x+1,p+stoi(cnt),s.substr(x,1),k+1);
    cnt.push_back(s[x]);
    if(stoi(cnt)+p<=n)dfs(x+1,p,cnt,k);
}
int main(){
    cin>>s>>n;
    dfs(0, 0, "0",0);
    if(ans==0x3f3f3f3f){
        cout<<-1;
        return 0;
    }
    cout<<ans;
    return 0;
}

本人已过却是卡时间加欧气

希望大佬帮助

2024/12/11 14:57
加载中...