Blue Bridge Cup competition scheduled training algorithm VIP questions

Questions algorithm training and competition schedule

Resource constraints
Time limit: 1.0s memory limit: 512.0MB
description of the problem
  has had 2 n (n <= 6) a team round robin competition, planned in 2 n - 1 days to complete, each team for a game day . Arrange for designing a game, so the 2 n - 1 days, each team with different opponents game.
Input Format
  Input File matchplan.in common line, the input value of n.
Output format
  output file matchplan.out Total (2 n - 1) line, the output of the i-th row i matchmaking day.
  Format: AB, CD, ....... Where i is the number of days, A, B are both numbers match, a total of each row of 2 n-1 th games played.
Input Sample
2
Sample Output
<1> 1-2, 3-4
<2> 1-3,2-4
<3> 1-4,2-3

Ideas: The problem lies in the difficulty of arrangement (the arrangement from the outset are, can not be changed, then one would also regulate this question, but doing it a little better) and how to record and raced team. First define two arrays, a record for the number of days the two teams in the past over whether the game, the other is used to record the game situation the day the two teams, then the number of days when the two teams meet in front of the day and did not contest the case, you can arrange together, and then change the value of each of the two arrays, said that it has raced, the value must be reset daily record of the day, another title should be changed to a comma-separated in space, on the evaluation of the data is such that otherwise might not pair .

code show as below:

#include<stdio.h>
#include<string.h>
#include<math.h>
int main(){
	int a[65][65],b[65],i,j,k,l,n,m; //数组定义大了可以防止数据溢出
	scanf("%d",&n);
	m=pow(2,n);
	memset(a,0,sizeof(a));
	for(i=1;i<=m-1;i++){
		printf("<%d>",i);
		memset(b,0,sizeof(b));
		for(j=1;j<=m/2;j++){
			for(k=1;k<=m;k++){
				int s=1;
				if(b[k]==1){
					continue;
				}
				else{
					b[k]=1;
				}
				for(l=1;l<=m;l++){
					if(b[l]==1){
						continue;
					}
					else{
						if(a[k][l]!=1 && b[l]!=1){
							printf("%d-%d ",k,l);
							a[k][l]=1;
							b[l]=1;
							s++;
							break;
						}
					}
				}
				if(s==2){
					s=1;
					break;
				}
			}
		}
		printf("\n");
	}
	return 0;
}
发布了51 篇原创文章 · 获赞 47 · 访问量 2012

Guess you like

Origin blog.csdn.net/weixin_45269353/article/details/104517658