C++ C (1152) : 循环赛日程表

文章目录


一、题目描述


二、参考代码

cpp 复制代码
#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;
}

相关推荐
碧海蓝天20221 分钟前
_Array类,类似于Vector,其实就是_string
数据结构·c++·算法
Satan7122 分钟前
【Java】全面理解Java8特性
java·开发语言
hakesashou8 分钟前
python有main函数吗
开发语言·python
Zach_yuan9 分钟前
C++重生之我是001
开发语言·c++
Antonio91517 分钟前
【高级数据结构】并查集
数据结构·c++·算法
打破砂锅问到底00728 分钟前
技术周总结 09.16~09.22 周日(架构 C# 数据库)
开发语言·架构
人生导师yxc32 分钟前
Java面向对象编程
java·开发语言
一只雪球球36 分钟前
【练习16】求最小公倍数
java·开发语言
繁依Fanyi43 分钟前
828华为云征文|华为Flexus云服务器搭建Cloudreve私人网盘
开发语言·人工智能·pytorch·python·算法·华为云·php
YuCaiH1 小时前
【C语言-数据结构】单链表的定义
c语言·数据结构·笔记