循环赛日程安排问题:设有n=2^k个选手要进行网球循环赛,要求设计一个满足以下要求的比赛日程表: (1)每个选手必须与其他n-1个选手各赛一次(2)每个选手一天只能赛一次。 按此要求,可将比赛日程表设计成一个 n行n-1列的二维表,其中,第i行第j列表示和第i个选手在第j天比赛的选手。请用c语言写出该算法
时间: 2023-06-08 07:05:30 浏览: 568
循环赛问题分析与C语言代码-分治法.doc
5星 · 资源好评率100%
循环赛日程安排算法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void schedule(int n)
{
int i, j, k;
int **table;
/* allocate memory for the schedule table */
table = (int **)malloc(n * sizeof(int *));
for (i = 0; i < n; i++)
table[i] = (int *)malloc((n-1) * sizeof(int));
/* initialize the table with -1 */
for (i = 0; i < n; i++)
for (j = 0; j < n-1; j++)
table[i][j] = -1;
/* fill in the table */
for (i = 0; i < n; i++)
{
table[i][0] = i;
for (j = 1, k = 1; j < n-1; j++, k++)
{
if (k == i)
k++;
if (k == n)
k = 0;
table[i][j] = k;
}
}
/* print the table */
printf("The schedule table is:\n");
for (i = 0; i < n; i++)
{
printf("Day %2d: ", i+1);
for (j = 0; j < n-1; j++)
printf("%2d ", table[i][j]);
printf("\n");
}
printf("\n");
/* free the memory */
for (i = 0; i < n; i++)
free(table[i]);
free(table);
}
int main()
{
int k, n;
/* read in k */
printf("Enter k: ");
scanf("%d", &k);
/* compute n */
n = 1 << k;
/* schedule the matches */
schedule(n);
return 0;
}
阅读全文