网球日程赛安排,集合划分问题

第二次算法作业

日程赛

#include<iostream>
#include<cstring>
using namespace std;
const int N=30;
int m[N][N];//日程表用二维数组表示,m[d][p]为第d天第p个选手被安排的对手序号,为0即此人此日轮空即无安排
int n;//比赛人数

//两个人比赛的安排
void settwo(int day,int player1,int player2)
{
	int p1=player1,p2=player2;
	m[day][p1]=p2;
	m[day][p2]=p1;
}
//组间:firstPlayer1是第一小组的第一个选手编号,firstPlayer2是第二小组的第一个选手编号,
void setgroups(int firstDay,int firstPlayer1,int firstPlayer2,int playerNum,int matchingOff)
{//外层循环比赛的天数,如果matchingOff为1则说明有轮空情况,那么比赛的天数就在对该小组来说少一天
	for(int d=0;d<playerNum-matchingOff;d++)
	{
		for(int p=0;p<playerNum;p++)//所有选手编号
		{
			int p1=p+firstPlayer1;//第一小组组选手的编号
			int p2=firstPlayer2+(d+p+matchingOff)%playerNum;//第二小组选手编号,用除余模拟轮转保证编号有效
			settwo(firstDay+d,p1,p2);
		}
	}
}
//找轮空的人
int find(int day,int firstPlayer,int playerNum)
{
	for(int i=firstPlayer;i<firstPlayer+playerNum;i++)
	{
		if(m[day][i]==0)
		{
			return i;
		}
	}
}

//安排选手firstPlayer从firstDay开始比赛,比赛一共有playerNum个人,的日程
void arrange(int firstDay,int firstPlayer,int playerNum)
{
	//特殊情况,提前处理
	if(playerNum==2)
	{
		settwo(firstDay,firstPlayer,firstPlayer+1);
		return;
	}
	
	if(playerNum%2==0)
	{
		int halfNum=playerNum/2;
		//分治:偶数正好分两个组,组内比
		arrange(firstDay,firstPlayer,halfNum);
		arrange(firstDay,firstPlayer+halfNum,halfNum);
		
		//组间
		if(halfNum%2==0)
		{
			setgroups(firstDay+halfNum-1,firstPlayer,firstPlayer+halfNum,halfNum,0);
		}
		else{
			for(int d=firstDay;d<firstDay+halfNum;d++)
			{
				int p1=find(d,firstPlayer,halfNum);
				int p2=find(d,firstPlayer+halfNum,halfNum);
				settwo(d,p1,p2);
			}
			setgroups(firstDay+halfNum,firstPlayer,firstPlayer+halfNum,halfNum,1);
		}
	}
	else{
		int playerNum1=playerNum+1;//补上一个人
		int halfNum=playerNum1/2;
		
		arrange(firstDay,firstPlayer,playerNum1);
		
		int f=firstPlayer+playerNum;
		
		for(int d=firstDay;d<firstDay+playerNum;d++)
		{
			int rival=m[d][f];
			m[d][rival]=0;
			m[d][f]=0;
		}
	}
}

int main()
{
	cout<<"please give the number of players:";
	cin>>n;
	
	//安排
	arrange(1,1,n);
	
	//输出
	//n为偶数个人比赛n-1天;
	//奇数个人处理:无中生有一个人变成偶数个人即n+1,由于偶数个人比赛n-1天,故最终奇数个人比赛n+1-1=n天
	//我需要在乎我无中生有的那个人吗,不需要,因为安排表里面和这个人比赛的人的安排至空即可
	int days;
	if(n%2==0)
	{
		days=n-1;
	}else days=n;
	
	cout<<"PlayerNumber";
	
	for(int i=1;i<=days;i++)
	{
		printf(" day%02d",i);
	}
	
	cout<<endl;
	
	for(int i=1;i<=n;i++)
	{
		printf("     %02d     ",i);
		for(int j=1;j<=days;j++)
		{
			if(m[j][i]==0)
			{
				cout<<"   -  ";
			}else printf("   %02d ",m[j][i]);
		}
		cout<<endl;
	}
	
	return 0;
}

集合划分

#include<iostream>
#include<fstream>
using namespace std;

//处理n个元素划分成k个子集
int f(int n,int k)
{
	if(n==k||k==1)
	{
		return 1;
	}
	return f(n-1,k-1)+k*f(n-1,k);
}
int main()
{
	int n,m,ans=0;
	ifstream inputfile("input.txt");
	if(inputfile.is_open())
	{
		inputfile>>n>>m;
		inputfile.close();
	}else{
		cerr<<"无法打开输入文件"<<endl;
		return 1;
	}
	
	ans=f(n,m);
	
	ofstream outputfile("output.txt");
	if(outputfile.is_open())
	{
		outputfile<<ans;
		outputfile.close();
	}else{
		cerr<<"无法打开输出文件"<<endl;
		return 1;
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值