文具店(计蒜客)
1 #include<iostream> 2 #include<stdlib.h> 3 #include<math.h> 4 using namespace std; 5 int ma=0x3f3f3f3f;//0x3f3f3f3f 6 void dfs(string s,int len,int n,int k,int now) 7 { 8 if(k==1) 9 { 10 for(int i=n;i<len;++i) 11 { 12 now += (s[i]-'0')*pow(10,len-i-1); 13 } 14 if(now<ma) 15 ma=now; 16 } 17 if(k>1) 18 { 19 for(int i=n;i<=len;++i) 20 { 21 if(i==len) 22 { 23 dfs(s,len,n,k-1,now); 24 } 25 else{ 26 int plus = 0; 27 for(int j=n;j<=i;++j) 28 { 29 plus += (s[j]-'0')*pow(10,i-j); 30 } 31 dfs(s,len,i+1,k-1,now+plus); 32 } 33 } 34 } 35 } 36 int main() 37 { 38 int k; 39 string s; 40 cin>>s; 41 cin>>k; 42 int len = s.length(); 43 dfs(s,len,0,k,0); 44 cout<<ma; 45 }