- 注册时间
- 2009-7-10
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 41
- 在线时间
- 小时
|
楼主 |
发表于 2009-7-10 23:12:10
|
显示全部楼层
- ============Part2---------main.cpp==============================
- #include "head.h"
-
- //求把30个数的数组分解为10个Group的算法
- //思量是:先将30个数组合3,一共有4060个3组合。
- //然后再在这4060个组合里面依次选出第1个Group,第2个Group....第10个Group
- //选择的范围就是在上面组合出来的4060个3组合里面选,选第n+1个Group的时候排除掉和
- //第1 - n组合3有重复的3组合(就是只选前面没有出现的Group)来保证各个Group是互相独立的
- //没有交集。
- long Get30Group10(void) const {
- long total = 0;
- std::vector<ThreeCodes> threeCodeResult;
- int howManyThreeCodeResult = 0;
- int a[30];
- for(int i=0;i<30;i++) a[i] = i+1;
-
- //howManyThreeCodeResult is 4060
- howManyThreeCodeResult = combineCodes(a,30,3,threeCodeResult);
-
- std::set<int> reSet;
- std::set<int> tmpSet;
- std::set<int>::iterator reIter;
-
- for(int i=0;i<howManyThreeCodeResult;i++){
- //求一种分拆方式,分拆的10个Group放入oneDivided中
- OneDivided oneDivided;
- //选择Group0
- oneDivided.group[0].setCodes(threeCodeResult[i].r1,
- threeCodeResult[i].r2,
- threeCodeResult[i].r3);
- for(int j=0;j<howManyThreeCodeResult;j++) {
- tmpSet.clear();
- tmpSet.insert(tmpSet.begin(),threeCodeResult[j].r1);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[j].r2);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[j].r3);
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[0].redSet.begin(),oneDivided.group[0].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
-
- if(reSet.size() > 0) {
- //有相同
- continue;
- }
- //选择Group1, 在选择Group1之前,先排除和Group0有重复的元素,下面其他类似
- oneDivided.group[1].setCodes(threeCodeResult[j].r1,
- threeCodeResult[j].r2,threeCodeResult[j].r3);
-
- for(int k=0;k<howManyThreeCodeResult;k++) {
- tmpSet.clear();
- tmpSet.insert(tmpSet.begin(),threeCodeResult[k].r1);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[k].r2);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[k].r3);
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[0].redSet.begin(),oneDivided.group[0].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size0 = reSet.size();
- if(size0 > 0) {
- continue;
- }
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[1].redSet.begin(),oneDivided.group[1].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size1 = reSet.size();
-
- if(size1 > 0) {
- //有相同
- continue;
- }
- oneDivided.group[2].setCodes(threeCodeResult[k].r1,
- threeCodeResult[k].r2,threeCodeResult[k].r3);
-
- for(int m=0;m<howManyThreeCodeResult;m++) {
- tmpSet.clear();
- tmpSet.insert(tmpSet.begin(),threeCodeResult[m].r1);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[m].r2);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[m].r3);
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[0].redSet.begin(),oneDivided.group[0].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size0 = reSet.size();
- if(size0 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[1].redSet.begin(),oneDivided.group[1].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size1 = reSet.size();
- if(size1 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[2].redSet.begin(),oneDivided.group[2].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size2 = reSet.size();
- if(size2 > 0) {
- continue;
- }
- oneDivided.group[3].setCodes(threeCodeResult[m].r1,
- threeCodeResult[m].r2,threeCodeResult[m].r3);
- for(int n=0;n<howManyThreeCodeResult;n++) {
- tmpSet.clear();
- tmpSet.insert(tmpSet.begin(),threeCodeResult[n].r1);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[n].r2);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[n].r3);
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[0].redSet.begin(),oneDivided.group[0].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size0 = reSet.size();
- if(size0 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[1].redSet.begin(),oneDivided.group[1].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size1 = reSet.size();
- if(size1 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[2].redSet.begin(),oneDivided.group[2].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size2 = reSet.size();
- if(size2 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[3].redSet.begin(),oneDivided.group[3].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size3 = reSet.size();
- if(size3 > 0) {
- continue;
- }
-
- oneDivided.group[4].setCodes(threeCodeResult[n].r1,
- threeCodeResult[n].r2,threeCodeResult[n].r3);
-
- for(int o=0;o<howManyThreeCodeResult;o++) {
- tmpSet.clear();
- tmpSet.insert(tmpSet.begin(),threeCodeResult[o].r1);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[o].r2);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[o].r3);
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[0].redSet.begin(),oneDivided.group[0].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size0 = reSet.size();
- if(size0 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[1].redSet.begin(),oneDivided.group[1].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size1 = reSet.size();
- if(size1 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[2].redSet.begin(),oneDivided.group[2].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size2 = reSet.size();
- if(size2 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[3].redSet.begin(),oneDivided.group[3].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size3 = reSet.size();
- if(size3 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[4].redSet.begin(),oneDivided.group[4].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size4 = reSet.size();
- if(size4 > 0) {
- continue;
- }
-
- oneDivided.group[5].setCodes(threeCodeResult[o].r1,
- threeCodeResult[o].r2,threeCodeResult[o].r3);
-
- for(int p=0;p<howManyThreeCodeResult;p++) {
- tmpSet.clear();
- tmpSet.insert(tmpSet.begin(),threeCodeResult[p].r1);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[p].r2);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[p].r3);
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[0].redSet.begin(),oneDivided.group[0].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size0 = reSet.size();
- if(size0 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[1].redSet.begin(),oneDivided.group[1].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size1 = reSet.size();
- if(size1 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[2].redSet.begin(),oneDivided.group[2].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size2 = reSet.size();
- if(size2 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[3].redSet.begin(),oneDivided.group[3].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size3 = reSet.size();
- if(size3 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[4].redSet.begin(),oneDivided.group[4].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size4 = reSet.size();
- if(size4 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[5].redSet.begin(),oneDivided.group[5].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size5 = reSet.size();
- if(size5 > 0) {
- continue;
- }
-
- oneDivided.group[6].setCodes(threeCodeResult[p].r1,
- threeCodeResult[p].r2,threeCodeResult[p].r3);
- for(int s=0;s<howManyThreeCodeResult;s++) {
- tmpSet.clear();
- tmpSet.insert(tmpSet.begin(),threeCodeResult[s].r1);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[s].r2);
- tmpSet.insert(tmpSet.begin(),threeCodeResult[s].r3);
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[0].redSet.begin(),oneDivided.group[0].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size0 = reSet.size();
- if(size0 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[1].redSet.begin(),oneDivided.group[1].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size1 = reSet.size();
- if(size1 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[2].redSet.begin(),oneDivided.group[2].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size2 = reSet.size();
- if(size2 > 0) {
- continue;
- }
-
- reSet.clear();
- reIter = reSet.begin();
- set_intersection(oneDivided.group[3].redSet.begin(),oneDivided.group[3].redSet.end(),
- tmpSet.begin(), tmpSet.end(),
- inserter(reSet, reIter));
- int size3 = reSet.size();
- if(size3 > 0) {
- continue;
- }
- //===================================Part 2 End ===============================//
- //见下面(由于帖子长度有限制)
复制代码 |
|