#include<iostream>标签:int,子集合,void,整数,num,exit,DynamicClass,集合,include From: https://blog.51cto.com/u_15899033/5903531
#include<vector>
#include<cstdlib>
using namespace std;const int Max=10000;
class DynamicClass
{
private:
int n; //n表示{1,2,3,...n}
long s; //表示1+2+...+n
vector<int>v; //v[i]表示累加和是i的组合数,如v[3]=2表示{1,2},{3}两种组合
public:
DynamicClass();
void Divide(int &num); //num表示有多少种分法
};DynamicClass::DynamicClass()
{
int x;
cout<<"请输入N:";
cin>>x;
if(!cin.good())
{
cerr<<"输入格式错误!"<<endl;
exit(0);
}
if(x>=Max || x<=0)
{
cerr<<"输入的值应该在0到"<<Max<<"之间!"<<endl;
exit(0);
}
this->n=x;
this->s=n*(n+1)/2;
if(s%2)
{
cerr<<"不能划分成值相等的两个子集合!"<<endl;
exit(0);
}
int i;
for(i=0;i<=s;i++) this->v.push_back(0);
this->v[0]=1; //很重要
}void DynamicClass::Divide(int &num)
{
int i,j;
int k=s/2; //k是每个子集合所有值之和
for(i=1;i<=this->n;i++) //将第1,2,...n个元素依次取出
{
for(j=k;j>=i;j--)
{
this->v[j]+=this->v[j-i]; //动态规划,后一阶段取决于前一阶段
}
for(int t=1;t<=k;t++) cout<<this->v[t]<<" ";
cout<<endl;
}
num=this->v[k]/2;
}void main()
{
DynamicClass dc;
int num=0;
dc.Divide(num);
cout<<"有"<<num<<"种分法"<<endl;
}