码迷,mamicode.com
首页 > 其他好文 > 详细

uva-165-枚举

时间:2017-10-08 00:15:12      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:int   arc   stream   i++   turn   blog   open   ++   final   

题意:选取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;
}

  

uva-165-枚举

标签:int   arc   stream   i++   turn   blog   open   ++   final   

原文地址:http://www.cnblogs.com/shuiyonglewodezzzzz/p/7636197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!