“循环赛日程安排”问题的分而治之解决算法
2007-10-05 21:54
253 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
标题:<<系统设计师>>应试编程实例-[分而治之算法程序设计]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
作者:成晓旭
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
时间:2002年09月15日(11:58:00-13:18:00)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
实现“装箱”问题的贪婪算法实现函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdio.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdlib.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:====================“循环赛日程安排”问题的分而治之解决算法====================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
作者:成晓旭
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
时间:2002年09月15日(11:58:38-20:00:00)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
完成“循环赛日程安排”问题的分而治之解决算法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
===================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
问题描述:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
设有n(n = 2^k)位选手参加网球循环赛,循环赛共进行n-1天,每位选手要与
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
其他n-1位选手比赛一场,且每位选手每天必须比赛一场,不能轮空。试按此要求
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
为比赛安排日程。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
编程思想:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
假设n位选手被顺序编号为1,2,3,...,n,比赛的日程表是一个n行n-1列的表格,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i行j列的表格内容是第i号选手在第j天的比赛对手。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
根据分而治之的原则,可从其中一半选手(2^(n-1位)的比赛日程,导出全体n位
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
选手的日程,最终细分到只有两位选手的比赛日程出发。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
可假设只有8位选手参赛,若1至4号选手之间的比赛日程填在日程表的左上角
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(4行3列),5至8号选手之间的比赛日程填在日程表的左下角(4行3列);那么左下角的
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
内容可由左上角的对应项加上数字4得到。至此,剩余的右上角(4行4列)是为编号小的
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
1至4号选手与编号大的5至8号选手之间的比赛日程安排。例如,在第4天,让1至4号选
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
手分别与5至8号选手比赛,以后各天,依次由前一天的日程安排,让5至8号选手“循环
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
轮转”即可。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
最后,比赛日程表的右下角的比赛日程表可由,右上角的对应项减去数字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
4得到。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
编程图例:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
===================================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 选手 1天 2天 3天 4天 5天 6天 7天 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
===================================================================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 1号 | 2 | 3 | 4 || 5 | 6 | 7 | 8 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 2号 | 1 | 4 | 3 || 6 | 7 | 8 | 7 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 3号 | 4 | 1 | 2 || 7 | 8 | 5 | 6 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 4号 | 3 | 2 | 1 || 8 | 5 | 6 | 5 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
========[左上角]========================[右上角]===================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 5号 | 6 | 7 | 8 || 1 | 4 | 3 | 2 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 6号 | 5 | 8 | 7 || 2 | 1 | 4 | 3 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 7号 | 8 | 5 | 6 || 3 | 2 | 1 | 4 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
|*| 8号 | 7 | 6 | 5 || 4 | 3 | 2 | 1 |*|
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
========[左下角]========================[右下角]===================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#define MAXN 64
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//日程表数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int calendar[MAXN + 1][MAXN];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
void Round_Robin_Calendar()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i,j,m,number,p,q;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("输入选手个数:(注意:2^k) ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d",&number);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//预置两位选手的比赛日程表://第i位选手第j天与第calendar[i][j]位选手比赛
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
calendar[1][1] = 2; //第1位选手第1天与第2位选手比赛
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
calendar[2][1] = 1; //第2位选手第1天与第1位选手比赛
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(m < number)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m ++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//p = p + p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p += p;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q = 2 * p; //为2^m位选手安排比赛日程
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//填充日程表的左下角
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = p + 1;i <= q;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j = 1;j<= p - 1;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
calendar[i][j] = calendar[i - p][j] + p; //左下角的内容 = 左上角的对应项加上数字4[]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//填充日程表的右上角
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//填充日程表的右上角的第1列
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
calendar[1][p] = p + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 2;i <= p;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
calendar[i][p] = calendar[i - 1][p] + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//填充日程表的右上角的其他列,参照前一列填充当前列[循环轮转算法]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j = p + 1;j < q;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 1;i < p;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
calendar[i][j] = calendar[i + 1][j - 1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
calendar[p][j] = calendar[1][j - 1];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//填充日程表的右下角
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j = p;j < q;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 1;i <= p;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
calendar[calendar[i][j]][j] = i; //关键语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 1;i <= q;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(j = 1;j < q;j++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%4d",calendar[i][j]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//:====================“循环赛日程安排”问题的分而治之解决算法====================
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Round_Robin_Calendar();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf(" 应用程序运行结束! ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- “循环赛日程安排”问题的分而治之解决算法
- 算法-分治法实例:循环赛日程安排问题
- 算法——使用递归解决组合问题
- “马的遍历”问题的贪婪法解决算法
- “装箱”问题的贪婪法解决算法