範例程式碼 uva165
//uva165
#include <bits/stdc++.h>
using namespace std;
int h,k,ans[15],maxValue;
bool check(int target,int curStamp,int usedStamp, int sum,int stamps[],int end){
if(sum == target) return true;
else if(curStamp == end || usedStamp == h) return false;
else if(check(target,curStamp+1,usedStamp,sum,stamps,end)) return true;
else if(check(target,curStamp,usedStamp+1,sum+stamps[curStamp],stamps,end)) return true;
return false;
}
void DFS(int curMax,int stamps[],int end){
if (check(curMax,0,0,0,stamps,end))
DFS(curMax+1,stamps,end);
if (end < k){
stamps[end] = curMax;
++end;
DFS(curMax+1,stamps,end);
--end;
stamps[end] = -1;
}
if (curMax-1 > maxValue){
maxValue = curMax-1;
memcpy(ans,stamps,sizeof(ans));
}
}
int main(){
int stamps[15];
while (cin >> h >> k){
if (h == 0 && k == 0) break;
for (int i = 0; i < 15; ++i)
stamps[i] = -1;
maxValue = -1;
DFS(1,stamps,0);
/*for( int i = 0; i < k; ++i)
cout << setw(3) << ans[i];
cout << " ->" << setw(3) << maxValue << endl;*/
cout<<maxValue<<endl;
}
return 0;
}