玄关求助,hack2和4过不去,其他都对
查看原帖
玄关求助,hack2和4过不去,其他都对
223333
propitious楼主2024/4/9 00:08
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#define ll long long
using namespace std;

string str;
ll n,ans=0x3f3f3f3f,len;
void dfs(int step,int num,int now,int add){
	if(num>ans || now>n || add>n) return;
	if(step > len) {
		if(now + add == n){
			ans=num;
		}
		return;
	}
	add = 10 * add + (str[step-1] -'0');
	dfs(step+1,num+1,now + add,0);
	dfs(step+1,num,now,add); 
} 

int main(){
	cin>>str;
	cin>>n;
	len=str.length();
	dfs(1,0,0,0);
	if(ans<len)
	cout<<ans<<endl;
	else cout<<-1;
	return 0;
}
2024/4/9 00:08
加载中...