循环比赛

Problem Description

设有n个选手进行循环比赛,其中n = 2m,要求每名选手要与其他n-1名选手都赛一次,每名选手每天比赛一次,循环赛共进行n - 1天,要求每天没有选手轮空。

Input Format

一个正整数m(2 <= m <= 6)。

Output Format

样例形式的比赛安排表。

Algorithm Design

      模拟

Problem Analysis

模拟:

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

题目样例暴露了规律:每2^i行的下一行就要针对上一行进行长度为2^i单位之间的调换,直接进行O(mn^2)的模拟就可以过。

Source Code

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int m,num[1001];
 6 
 7 int check(int x)
 8 {
 9     int mi=0;
10     while(x)
11     {
12         if((x&1))return mi;
13         x/=2;
14         mi++;
15     }
16     return mi;
17 }
18 
19 int main()
20 {
21     freopen("match.in","r",stdin);
22     freopen("match.out","w",stdout);
23     cin>>m;
24     for(int i=1;i<=(1<<m);i++)
25     {
26         num[i]=i;
27         cout<<i<<" ";
28     }
29     cout<<endl;
30     for(int i=2;i<=(1<<m);i++)
31     {
32         int last=check(i-1);
33         for(int j=0;j<=last;j++)
34         for(int k=1;k<=(1<<m);k+=(1<<j+1))
35         for(int f=1;f<=(1<<j);f++)
36             swap(num[k+f-1],num[k+f-1+(1<<j)]);
37         for(int j=1;j<=(1<<m);j++)
38             cout<<num[j]<<" ";
39         cout<<endl;
40     }
41     return 0;
42 }
View Code
原文地址:https://www.cnblogs.com/qswx/p/9411313.html