循环赛日历表(递归与非递归)

1.题目要求

设有n(n为2的k次幂)个运动员参加网球循环赛,现设计一个比赛日程表,要求:
(1)每个选手必须与其他n-1个选手各赛一次
(2)每个选手一天只能比赛一次
(3)循环赛一共进行n-1天

2.样例

Input
3
Output
1 2 3 4 5 6 7 8
2 1 4 3 6 5 8 7
3 4 1 2 7 8 5 6
4 3 2 1 8 7 6 5
5 6 7 8 1 2 3 4
6 5 8 7 2 1 4 3
7 8 5 6 3 4 1 2
8 7 6 5 4 3 2 1
Sample Input
2
Sample Output
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1

3.非递归


```cpp
#include<bits/stdc++.h>
void table(int k){
	int i,j,m,temp;
	int a[100][100];
	int n=2;//人数为2时直接打印矩阵
	a[1][1]=1;a[1][2]=2;
	a[2][1]=2;a[2][2]=1;
for(m=1;m<k;m++)  //系数
	{		
	temp=n;
	n=n*2;		
		for(i=temp+1;i<=n;i++)
		{
			for(j=1;j<=temp;j++)
			{
				a[i][j]=a[i-temp][j]+temp;	//在人数为2的矩阵数值基础上加			
			}
		}//左下角填充
		
		for(i=1;i<=temp;i++)
		{
			for(j=temp+1;j<=n;j++)
			{
				a[i][j]=a[i+temp][j-temp];			
			}
		}//右上角填充

		for(i=temp+1;i<=n;i++)
		{
			for(j=temp+1;j<=n;j++)
			{
				a[i][j]=a[i-temp][j-temp];				
			}
		}//右下角填充

	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			printf("%d ",a[i][j]);
			if(j==n)//打印完第一行
			{
				printf("\n");
			}
		}
	}
}
int main()
{
	int k;
	scanf("%d",&k);
	if(k!=0)
	table(k);
  system("pause");
}

4.递归

#include<iostream>
using namespace std;
int a[4001][4001],m=2;
void Display()//print 
{
 for(int i=1;i<=m;i++)
 {for(int j=1;j<=m;j++)
 printf("%d ",a[i][j]);
 cout<<endl;
 }
}
void create(int n)
{ 
 if(n==1)return;
 else
 {
int temp =m;
m*=2;
for(int i=1;i<=temp;i++)  //加矩阵右上
for(int j=1;j<=temp;j++)
a[i][j+temp]=a[i][j]+temp;

for(int i=1;i<=m;i++)   //加矩阵左下
for(int j=1;j<=m;j++)
a[i+temp][j]=a[i][j+temp];

for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)       //加矩阵右下
a[i+temp][j+temp]=a[i][j];

create(n-1);
 }


}

int main()
{
  int k;
  cin>>k;
  a[1][1]=1;a[1][2]=2;
  a[2][1]=2;a[2][2]=1;
  create(k);
  Display();
  system("pause");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王也枉不了

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

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

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

打赏作者

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

抵扣说明:

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

余额充值