循环日程安排问题C++代码实现

一、问题

      有size=2^n个选手要进行网球循环赛,设计一个满足以下要求的比赛日程表。满足每个选手必须和其他n-1个选手各赛一次、每个选手每一天只能赛一次、循环赛在n-1天内结束。

        第一列就是各个选手的编号,二剩下的七列代表第一天到第七天,比如第1行第3列,意思就是:1号选手在第2天要和3号选手比赛。

        大家可能发现这个图标好像很有规律,其实我们完全可以不管题目的表述,专注于如何生成这样一个看起来挺吓人的表格,先来找规律。

二、代码实现

         show()为显示二维矩阵,solve()为主要代码。

        x,y代表分割后四个区域的左上角的坐标,size是当前的边长,num是用来进行填充的数字。

#include<stdio.h>
#include<iostream>
using namespace std;
#define max 100


void show(int a[max][max], int size) {
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++) {
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void solve(int size, int a[max][max],int x,int y,int num) {
	size = size / 2;
	if (size == 1) {
		a[x][y] = num;
		a[x][y + size] = num + size;
		a[x + size][y] = num + size;
		a[x + size][y + size] = num;
		return;
	}
	solve(size, a, x, y, num);
	solve(size, a, x+size, y + size, num);
	solve(size, a, x , y+size, num+size);
	solve(size, a, x + size, y, num+size);
}

void solve(int size, int a[max][max],int x,int y,int num) {
	size = size / 2;
	if (size == 1) {
		a[x][y] = num;
		a[x][y + size] = num + size;
		a[x + size][y] = num + size;
		a[x + size][y + size] = num;
		return;
	}
	solve(size, a, x, y, num);
	solve(size, a, x+size, y + size, num);
	solve(size, a, x , y+size, num+size);
	solve(size, a, x + size, y, num+size);
}

int main() {
	int a[max][max];
	memset(a, 0, sizeof(a));
	int size = 8;
	int num = 1;
	solve(size, a, 0, 0,num);
	show(a, size);
	return 0;
}


 

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大王算法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值