一、题目描述
二、参考代码
# include <iostream>
# include <vector>
# include <cstdlib>
using namespace std; void generateSchedule ( vector< vector< int > > & table, int numPlayers, int rounds) { for ( int i = 1 ; i <= numPlayers; i++ ) { table[ 1 ] [ i] = i; } int matchGroupSize = 1 ; for ( int round = 1 ; round <= rounds; round++ ) { numPlayers /= 2 ; for ( int group = 1 ; group <= numPlayers; group++ ) { for ( int i = 1 + matchGroupSize; i <= 2 * matchGroupSize; i++ ) { for ( int j = 1 + matchGroupSize; j <= 2 * matchGroupSize; j++ ) { table[ i] [ j + ( group - 1 ) * matchGroupSize * 2 ] = table[ i - matchGroupSize] [ j + ( group - 1 ) * matchGroupSize * 2 - matchGroupSize] ; table[ i] [ j + ( group - 1 ) * matchGroupSize * 2 - matchGroupSize] = table[ i - matchGroupSize] [ j + ( group - 1 ) * matchGroupSize * 2 ] ; } } } matchGroupSize *= 2 ; }
} int calculateRounds ( int numPlayers, int rounds) { do { numPlayers = numPlayers / 2 ; rounds++ ; } while ( numPlayers > 1 ) ; return rounds;
} void printSchedule ( vector< vector< int > > & table, int numPlayers) { for ( int i = 1 ; i <= numPlayers; i++ ) { for ( int j = 2 ; j <= numPlayers; j++ ) { cout << table[ i] [ j] << " " ; } cout << endl; }
} int main ( ) { int rounds = 0 ; int numPlayers = 0 ; cin >> numPlayers; vector< vector< int > > v ( numPlayers + 1 , vector < int > ( numPlayers + 1 ) ) ; rounds = calculateRounds ( numPlayers, rounds) ; generateSchedule ( v, numPlayers, rounds) ; printSchedule ( v, numPlayers) ; return 0 ;
}