#include<iostream> #include<algorithm> #include<iterator> #include<set> using namespace std; /* *整数划分问题并显示每一种分法 */ set<multiset<int>> GetAllIntDivision(int n) { set<multiset<int>> allDivision; if(1==n) { multiset<int> aa; aa.insert(1); allDivision.insert(aa); } else { set<multiset<int>> iniDivision = GetAllIntDivision(n-1); for(set<multiset<int>>::iterator iter = iniDivision.begin();iter != iniDivision.end();iter++) { //加入后缀项 multiset<int> inimul; inimul = *iter; inimul.insert(1); allDivision.insert(inimul); //加入累加项 /*inimul = *iter;*/ for (multiset<int>::iterator it = (*iter).begin();it!=(*iter).end();++it) { multiset<int>::iterator iit = it; inimul = *iter; while((++iit)!=(*iter).end()&&(*it)==(*iit)) { iit = (++it); } inimul.erase(inimul.find(*it)); inimul.insert((*it)+1); allDivision.insert(inimul); } } } return allDivision; } int main() { int n; cin>>n; set<multiset<int>> allDivision; allDivision = GetAllIntDivision(n); //输出结果 int i = 0; for(set<multiset<int>>::iterator iter = allDivision.begin();iter != allDivision.end();iter++) { cout<<"第"<<++i<<"种划分: "; copy((*iter).begin(),(*iter).end(),ostream_iterator<int>(cout," ")); cout<<endl; } return 0; }