算法(c++)—— 集合划分问题

目录

一、问题描述

 二、代码

三、结果

四、完整程序代码包


一、问题描述

 二、代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<string>
#include<stdio.h>

using namespace std;


string r(string s, string c, string y){
    int n = c.length();
    int m = s.length();
    string ss = "";
    for(int pos = 0,p = 0; pos < m;p++, pos++){
        if(s.substr(pos, n) == c){
            ss = ss + y;
            pos = pos + n - 1;
        }
        else ss.push_back(s[pos]);
    }
    return ss;
}
string spt(string s, char x){
    int m = s.length();
    string ss = "";
    for(int i = 0; i < m; i++){
        if(s[i]==x){
            break;
        }
        else ss.push_back(s[i]);
    }
    return ss;
}
void  split(string sp,string st,int max){
        if(st==""){
            cout<<sp<<endl;
            return;
        }
        string  temp[] = {spt(sp, '|')};
        for(int i=0;i<st.length();i++){
            int code=st[i];
            string c = "";
            c.push_back(st[i]);
            st=r(st, c, "");
            if(code>max){
                if(!(sp=="")){
                    for(int j=0;j < sizeof(temp) / sizeof(temp[0]);j++){
                        string d = "";
                        d = d +c+temp[j];
                        string f = "";
                        f = f + r(sp, temp[j],d);
                        split(f,st,code);
                    }
                }
                string e = "";
                string str = "";
                e = e + sp+'|'+c;
                str=(sp=="")?c:e;
                split(str,st,code);
            }
        }
    }

//给定正整数n 和m,计算出n 个元素的集合{1,2,., n }可以划分为多少个不同的由m 个非空子集组成的集合。
long long q(long long m,long long n)
{
    if(m==1)
        return 1;
    if(m==n)
        return 1;
    else
        return q(m-1,n-1)+q(m,n-1)*m;
}

int main()
{
    long i;
    long n,sum=0;
    cin>>n;
    cout<<endl;
    for(i=1; i<=n; i++){
        sum+=q(i,n);
    }
    cout<<sum<<endl<<endl;
    split("", "123456",-1);
    return 0;
}

三、结果

四、完整程序代码包

suanFa2_7.rar-讲义文档类资源-CSDN下载算法(c++)——集合划分问题更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/lcf0000/55663711

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高冷男孩不吃苹果

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值