题意:选取k种面额的邮票,总数是h,要求组合出来的连续数最大
枚举,网上看到一个更快的等价类划分,留着学等价类划分的思路
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<map>
#include<memory.h>
#include <math.h>
#include<time.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int N = 200;
int res[N], stamp[N], m[N];
int h, k;
int final = -1;
int vis[N];
void dfs(int cur, int n, int sum)
{
if(cur == h)
{
vis[sum] = 1;
return;
}
vis[sum] = 1;
for(int i = 0; i <= n; i++)
dfs(cur + 1, n, sum + stamp[i]);
}
void search(int cur)
{
if(cur == k)
{
if(m[cur - 1] > final)
{
final = m[cur - 1];
memcpy(res, stamp, sizeof(res));
}
return;
}
for(int i = stamp[cur-1] + 1; i <= m[cur - 1] + 1; i++)
{
memset(vis, 0, sizeof(vis));
stamp[cur] = i;
dfs(0, cur, 0);
int j = 1;
int num = 0;
while (vis[j++])
num++;
m[cur] = num;
search(cur + 1);
}
}
int main()
{
freopen("d:\\1.txt", "r", stdin);
while (cin >> h >> k && h && k)
{
m[0] = h;
stamp[0] = 1;
final = -1;
search(1);
for(int i = 0; i < k;i++)
{
printf("%3d",res[i]);
}
printf(" ->%3d\n",final);
}
return 0;
}