一个 很棘手的 分组问题 用c++有解决的办法吗?

kasimokdada 2009-05-01 03:36:27
m个人,每个人需要 用3名导师
导师 总数 为n

要求(1)任意两组的导师尽量不同
(2)任意两组不出现有两个 导师相同的情况
对于m n,给出两者关系
或者其 个别取值
...全文
333 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
shulinsen 2009-05-02
  • 打赏
  • 举报
回复
15楼写的运行成功了,感觉你好伟大
adventurelw 2009-05-02
  • 打赏
  • 举报
回复
15楼那个程序有点错误,实在不好意思
修正了一下:

//每个学生3名导师,任意两个学生的导师团队最多有一个人相同
std::vector<std::set<int> > slice(const std::vector<int> &ival);
int main()
{
using namespace std;
ofstream fout;
fout.open("note.txt");
if(!fout.is_open())
{
cerr << "File can not open.\n";
exit(EXIT_FAILURE);
}
cout << "请输入教授的数目(> 2,q to quit): ";
vector<int>::size_type pnumber;
while(cin >> pnumber && pnumber > 2)
{
vector<int> professor;
for(vector<int>::size_type i = 0; i < pnumber; ++i)
professor.push_back(i);
vector<set<int> > team_vec(slice(professor));
for(vector<set<int> >::size_type i = 0; i < team_vec.size(); ++i)
{
for(vector<set<int> >::size_type j = i + 1; j < team_vec.size(); ++j)
{
set<int> temp_set;
set_intersection(team_vec[i].begin(), team_vec[i].end(), team_vec[j].begin(),
team_vec[j].end(), inserter(temp_set, temp_set.begin()));
if(temp_set.size() == 2)
{
team_vec.erase(team_vec.begin() + j);
--j;
}
}
}
cout << pnumber << "个教授可以辅导" << team_vec.size() << "个学生。"
<< "分配如下:" << endl;
fout << pnumber << "个教授可以辅导" << team_vec.size() << "个学生。"
<< "分配如下:" << endl;
for(vector<set<int> >::size_type i = 0; i < team_vec.size(); ++i)
{
fout << "< ";
for(set<int>::iterator iter = team_vec[i].begin(); iter != team_vec[i].end(); ++iter)
fout << *iter << " ";
fout << ">" << endl;
}
fout << endl;
cout <<"请输入教授的数目(> 2,q to quit): ";
}
fout.close();
return 0;
}

std::vector<std::set<int> > slice(const std::vector<int> &ival)
{
typedef std::vector<int>::size_type size;
std::set<std::set<int> > rec_set;
for(size i = 0; i < ival.size() - 2; ++i)
{
std::set<int> temp;
temp.insert(ival[i]);
for(size j = i + 1; j < ival.size() - 1; ++j)
{
temp.insert(ival[j]);
for(size k = j + 1; k < ival.size(); ++k)
{
temp.insert(ival[k]);
rec_set.insert(temp);
temp.erase(ival[k]);
}
temp.erase(ival[j]);
}
temp.erase(ival[i]);
}
std::vector<std::set<int> > rec_vec(rec_set.begin(), rec_set.end());
return rec_vec;
}


同时增加了写入文件的功能。
并且昨天的结果也是有问题的,好像还不是线性关系。。。。
一些结果如下,现在也不知道是否正确了。。
3个教授可以辅导1个学生。分配如下:
< 0 1 2 >

6个教授可以辅导4个学生。分配如下:
< 0 1 2 >
< 0 3 4 >
< 1 3 5 >
< 2 4 5 >

9个教授可以辅导8个学生。分配如下:
< 0 1 2 >
< 0 3 4 >
< 0 5 6 >
< 0 7 8 >
< 1 3 5 >
< 1 4 6 >
< 2 3 6 >
< 2 4 5 >

10个教授可以辅导10个学生。分配如下:
< 0 1 2 >
< 0 3 4 >
< 0 5 6 >
< 0 7 8 >
< 1 3 5 >
< 1 4 6 >
< 1 7 9 >
< 2 3 6 >
< 2 4 5 >
< 2 8 9 >

20个教授可以辅导45个学生。分配如下:
< 0 1 2 >
< 0 3 4 >
< 0 5 6 >
< 0 7 8 >
< 0 9 10 >
< 0 11 12 >
< 0 13 14 >
< 0 15 16 >
< 0 17 18 >
< 1 3 5 >
< 1 4 6 >
< 1 7 9 >
< 1 8 10 >
< 1 11 13 >
< 1 12 14 >
< 1 15 17 >
< 1 16 18 >
< 2 3 6 >
< 2 4 5 >
< 2 7 10 >
< 2 8 9 >
< 2 11 14 >
< 2 12 13 >
< 2 15 18 >
< 2 16 17 >
< 3 7 11 >
< 3 8 12 >
< 3 9 13 >
< 3 10 14 >
< 3 15 19 >
< 4 7 12 >
< 4 8 11 >
< 4 9 14 >
< 4 10 13 >
< 4 16 19 >
< 5 7 13 >
< 5 8 14 >
< 5 9 11 >
< 5 10 12 >
< 5 17 19 >
< 6 7 14 >
< 6 8 13 >
< 6 9 12 >
< 6 10 11 >
< 6 18 19 >
kasimokdada 2009-05-02
  • 打赏
  • 举报
回复
好了吗?
kasimokdada 2009-05-02
  • 打赏
  • 举报
回复
kasimok@163.com
kasimokdada 2009-05-02
  • 打赏
  • 举报
回复
呵呵 我需要数据验证我建的数学模型 给我 二三十组好不??
adventurelw 2009-05-02
  • 打赏
  • 举报
回复
这样吧,你有邮箱的话,把可执行文件发给你
win32程序,应该没有执行的环境限制吧。
kasimokdada 2009-05-02
  • 打赏
  • 举报
回复
可以 给我 成功运行 得出的数据吗??
adventurelw 2009-05-02
  • 打赏
  • 举报
回复
数据给你没有意义啊
回归之后就是m = n - 2啊。
也即按照要求,导师数至少要比学生多两个。
kasimokdada 2009-05-02
  • 打赏
  • 举报
回复
我急需 数据 你成功运行了 能 给我 点数据吗????
SimonYeung 2009-05-02
  • 打赏
  • 举报
回复
对于这类问题一直苦手,进来学习一下大牛们的思路
hj_huangjun 2009-05-01
  • 打赏
  • 举报
回复
强人很多啊
adventurelw 2009-05-01
  • 打赏
  • 举报
回复
我不知道错误在哪,我是在vs2008上运行的,没有问题
而且,i并没有多次初始化啊。。。。。。
kasimokdada 2009-05-01
  • 打赏
  • 举报
回复
大侠 我运行有错啊 好人做到底吧..
h.cpp
c:\documents and settings\administrator\h.cpp(17) : error C2374: 'i' : redefinition; multiple initialization
c:\documents and settings\administrator\h.cpp(14) : see declaration of 'i'
d:\include\xtree(118) : warning C4786: 'std::_Tree<std::set<int,std::less<int>,std::allocator<int> >,std::set<int,std::less<int>,std::allocator<int> >,std::set<std::set<int,std::less<int>,std::allocator<int> >,std::less<std::set<int,std::less<int>,s
td::allocator<int> > >,std::allocator<std::set<int,std::less<int>,std::allocator<int> > > >::_Kfn,std::less<std::set<int,std::less<int>,std::allocator<int> > >,std::allocator<std::set<int,std::less<int>,std::allocator<int> > > >' : identifier was tr
uncated to '255' characters in the debug information
d:\include\set(33) : see reference to class template instantiation 'std::_Tree<std::set<int,std::less<int>,std::allocator<int> >,std::set<int,std::less<int>,std::allocator<int> >,std::

llocator<int> > > >::iterator' to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.

h.obj - 1 error(s), 0 warning(s)
  • 打赏
  • 举报
回复
(1)任意两组的导师尽量不同
(2)任意两组不出现有两个 导师相同的情况


觉得要分2部实现。
1.先保证2组的老师都完全不同,即A老师去了1组,就不能去2组。把老师分配完,看能不能满足一个学生3老师。
如果不满足则进行2.
2.保证两组不出现有两个 导师相同的情况下,开始从2组里引用1组的老师,1组引用2组的老师,直到满足一个学生3老师。
ToBeTough 2009-05-01
  • 打赏
  • 举报
回复
不懂
adventurelw 2009-05-01
  • 打赏
  • 举报
回复
总感觉要求不是太明确,尽量不相同也就是还是可以相同,如果只是要不完全相同,那么
m = n*(n-1)*(n-2)/6;

根据条件二,那么就是说任意两组最多一个导师相同,则答案同13楼, m = n - 2,代码随便写了一下,其实挺累的,可能算法有点笨吧

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
std::vector<std::set<int> > slice(const std::vector<int> &ival);
int main()
{
using namespace std;
cout << "请输入教授的数目(> 2,q to quit): ";
vector<int>::size_type pnumber;
while(cin >> pnumber && pnumber > 2)
{
vector<int> professor;
for(vector<int>::size_type i = 0; i < pnumber; ++i)
professor.push_back(i);
vector<set<int> > team_vec(slice(professor));
for(vector<set<int> >::size_type i = 0; i < team_vec.size(); ++i)
{
for(vector<set<int> >::size_type j = i + 1; j < team_vec.size(); ++j)
{
set<int> temp_set;
set_intersection(team_vec[i].begin(), team_vec[i].end(), team_vec[j].begin(),
team_vec[j].end(), inserter(temp_set, temp_set.begin()));
if(temp_set.size() == 2)
{
team_vec.erase(team_vec.begin() + j);
--j;
}
}
}
cout << pnumber << "个教授可以辅导" << team_vec.size() << "个学生。"
<< endl << "请输入教授的数目(> 2,q to quit): ";
}
return 0;
}

std::vector<std::set<int> > slice(const std::vector<int> &ival)
{
typedef std::vector<int>::size_type size;
std::set<std::set<int> > rec_set;
for(size i = 0; i < ival.size() - 2; ++i)
{
std::set<int> temp;
temp.insert(ival[i]);
for(size j = i + 1; j < ival.size() - 1; ++j)
{
temp.insert(ival[j]);
for(size k = j + 1; k < ival.size(); ++k)
temp.insert(ival[k]);
}
rec_set.insert(temp);
}
std::vector<std::set<int> > rec_vec(rec_set.begin(), rec_set.end());
return rec_vec;
}

shexinwei 2009-05-01
  • 打赏
  • 举报
回复
我是这样想的,先把所有的都付给前面的m/3个学生;后面的m/9个学生是在前面的每个里面取一个组成;依此类推
leo315 2009-05-01
  • 打赏
  • 举报
回复
少看了一个条件,
学生 老师
1 3
2 5
3 6
4 6
5 7
6 8
剩下的自己想想看看,找出每次增加一个学生,要增加多少老师就行了。
S_zxing 2009-05-01
  • 打赏
  • 举报
回复
帮顶……
lingyin55 2009-05-01
  • 打赏
  • 举报
回复
顶下
加载更多回复(10)

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧