当前位置: 首页 > news >正文

重复枚举和不重复枚举

重复枚举和不重复枚举

考虑一个这样的问题,对于一个5块钱,你有1,2,3,4,5块钱,任意多张,问有多少个不同方案数,把5块钱找开

枚举和等于5的序列即可,如果 2 3 和 3 2 是同一组,那么每次都从0位置开始枚举,如果 2 3 和 3 2不是同一组,那么,枚举开始位置必须为上一个位置。

代码如下

#include"pch.h"
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
    using std::cout;
    using std::endl;
    using std::cin;
    using std::map;
    using std::vector;
    using std::string;
    using std::sort;
    using std::priority_queue;
    using std::vector;
    using std::swap;
    using std::stack;
    using std::queue;
    using std::bitset;

      
    int total = 0;

    constexpr int N = 5;
    int dir[] = {1,2,3,4,5};
    int n;
    int ans[N];
    void dfsNotRepeat(int t,int pre,int index) 
    {
        if (t == 0)
        {
            for (int i = 0;i < index;i++)
                cout << " " << ans[i];
            cout << endl;
            return;
        }
        else
        {
            for (int i = pre;i < n;i++)
            {
                if (dir[i] <= t)
                {
                    ans[index] = dir[i];
                    dfsNotRepeat(t-dir[i],i,index+1);
                }
            }
        }


    }

    void dfsRepeat(int t, int pre, int index)
    {
        if (t == 0)
        {
            for (int i = 0;i < index;i++)
                cout << " " << ans[i];
            cout << endl;
            return;
        }
        else
        {
            for (int i = 0;i < n;i++)
            {
                if (dir[i] <= t)
                {
                    ans[index] = dir[i];
                    dfsRepeat(t - dir[i], i, index + 1);
                }
            }
        }



    }
    /**
    *
    *不重复的枚举
    *
    */
    void solve()
    {
        n = 5;
        dfsNotRepeat(5,0,0);

        cout << endl << endl << endl;
        dfsRepeat(5,0,0);
    }

};



int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
    //freopen("d://1.out", "w", stdout);
#endif // !ONLINE_JUDGE
    cc::solve();
    return 0;
}

 

posted on 2019-05-19 21:24 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10890898.html

相关文章:

  • c++初级(本人scdn)
  • hibernate5.3版本出现hibernate中The server time zone value“乱码”问题的解决办法。
  • linux测试某进程占用oi、cpu、内存的使用情况
  • 实验一 VLAN的创建
  • java版b2b2c社交电商spring cloud分布式微服务(十)高可用的服务注册中心
  • 如何真正学习好C语言
  • 6. 包机制
  • Android系统编译时集成三方APK
  • Sublime Text 快捷键(Windows)
  • 能冒泡的事件
  • BIT软件需求工程与UML建模课程第三周工作总结
  • T-Chat·活动预告 | 交流会:等保2.0新鲜发布,信息安全何去何从?
  • Selenium库详解
  • hdu4625 JZPTREE(斯特林数+dp)
  • 如何配置不同url打包-android实操篇
  • [译]如何构建服务器端web组件,为何要构建?
  • Angular 响应式表单之下拉框
  • IDEA常用插件整理
  • JS函数式编程 数组部分风格 ES6版
  • Object.assign方法不能实现深复制
  • python3 使用 asyncio 代替线程
  • Python打包系统简单入门
  • Python语法速览与机器学习开发环境搭建
  • spark本地环境的搭建到运行第一个spark程序
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 无服务器化是企业 IT 架构的未来吗?
  • 学习Vue.js的五个小例子
  • 原生Ajax
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (1)(1.13) SiK无线电高级配置(五)
  • (C++20) consteval立即函数
  • (Python) SOAP Web Service (HTTP POST)
  • (python)数据结构---字典
  • (编译到47%失败)to be deleted
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (九十四)函数和二维数组
  • (七)Knockout 创建自定义绑定
  • (四)鸿鹄云架构一服务注册中心
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转) 深度模型优化性能 调参
  • (转)iOS字体
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • .gitignore文件---让git自动忽略指定文件
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • ?.的用法
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析
  • @Service注解让spring找到你的Service bean
  • [ CTF ] WriteUp-2022年春秋杯网络安全联赛-冬季赛
  • [ 渗透测试面试篇 ] 渗透测试面试题大集合(详解)(十)RCE (远程代码/命令执行漏洞)相关面试题
  • [ActionScript][AS3]小小笔记
  • [CVPR2021]Birds of a Feather: Capturing Avian Shape Models from Images