题意:选取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-枚举的更多相关文章

  1. UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge

    题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...

  2. uva 165

    回溯  参考了一下别人的解法  1 必须存在  再枚举下一个数字的时候  从当前可取到的最小数字加一枚举到当前可取到的最大数字加一 /********************************* ...

  3. UVa 140 (枚举排列) Bandwidth

    题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...

  4. UVa 1151 (枚举 + MST) Buy or Build

    题意: 平面上有n个点,现在要把它们全部连通起来.现在有q个套餐,如果购买了第i个套餐,则这个套餐中的点全部连通起来.也可以自己单独地建一条边,费用为两点欧几里得距离的平方.求使所有点连通的最小费用. ...

  5. uva 165 Stamps

    题意: 现有k种邮票面额, 一封信上最多贴h张邮票. 求能贴出的最大连续区间,即[1, max_value]这个区间内的所有面额都能贴出来. 并输出k种面额, h + k <= 9. 思路: 这 ...

  6. Even Parity UVA - 11464 (枚举)

    从来没有觉得枚举有多费脑子的.但是这道题还是很香的. 思路:就是非常简单的枚举啦.   从一般的枚举开始考虑.一般的做法就是在所有的格子中有两种状态1, 0. 而一共有225个格子,所有一共要枚举的情 ...

  7. UVa 1354 枚举子集 Mobile Computing

    只要枚举左右两个子天平砝码的集合,我们就能算出左右两个悬挂点到根悬挂点的距离. 但是题中要求找尽量宽的天平但是不能超过房间的宽度,想不到要怎样记录结果. 参考别人代码,用了一个结构体的vector,保 ...

  8. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

  9. Uva 11754(枚举+中国剩余定理)

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  10. Java设计模式学习笔记,一:单例模式

    开始学习Java的设计模式,因为做了很多年C语言,所以语言基础的学习很快,但是面向过程向面向对象的编程思想的转变还是需要耗费很多的代码量的.所有希望通过设计模式的学习,能更深入的学习. 把学习过程中的 ...

随机推荐

  1. Nginx+Keepalive实现高可用负载均衡

    1.准备2台服务器 服务器名LB1,假设IP为192.168.1.100 服务器名LB2,假设IP为192.168.1.101 2.在LB1,LB2上分别安装Nginx 步骤参照:http://blo ...

  2. 【JUnit】@Test 报错,"Test cannot be resolved to a type"

    想用单元测试 JUnit 单元测试下写好的方法,发现写 @Test 标签报错了,"Test cannot be resolved to a type" 原来是项目没有导入 JUni ...

  3. ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)

    We consider problems concerning the number of ways in which a number can be written as a sum. If the ...

  4. Android开发小问题解决汇总

    1.从命名文件时报“Read-only file system”255|shell@jacinto6evm:/vendor/lib/hw $ sushell@xxx:/system/vendor/li ...

  5. streamsets 集成 cratedb 测试

    我们可以集成crate 到streamsets 中可以实现强大的数据导入,数据分析能力. 演示的是进行csv 文件的解析并输出到cratedb 环境使用docker && docker ...

  6. NSDate 时间加减

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/pearlhuzhu/article/details/26227393 NSDate有个类别,例如以下 ...

  7. 记录一下 FastAdmin getOriginData 问题

    记录一下 FastAdmin getOriginData 问题 FastAdmin 对 用户端新增了一个 money 字段,但在后台修改时出错,提示没有 getOriginData 方法. 跟踪了一下 ...

  8. 【转】每天一个linux命令(26):用SecureCRT来上传和下载文件

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/28/2793181.html 用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接 ...

  9. Oracle ASM 详解

    ASM:Automatic Storage Management, 是Oracle 主推的一种面向Oracle的存储解决方案, ASM 和 RDBMS 非常相似,ASM 也是由实例和文件组成, 也可以 ...

  10. vuex 知识点

    Action 类似于 mutation,不同在于: 1.Action 提交的是 mutation,而不是直接变更状态. 2.Action 可以包含任意异步操作. mutation是同步的,当需要异步操 ...