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;
}

相关推荐
ptu小鹏2 小时前
类和对象(中)
开发语言·c++
Bayi·4 小时前
前端面试场景题
开发语言·前端·javascript
碎梦归途4 小时前
23种设计模式-结构型模式之享元模式(Java版本)
java·开发语言·jvm·设计模式·享元模式
明月看潮生5 小时前
青少年编程与数学 02-018 C++数据结构与算法 06课题、树
数据结构·c++·算法·青少年编程·编程与数学
小指纹5 小时前
动态规划(一)【背包】
c++·算法·动态规划
Xiaoyu Wang5 小时前
Go协程的调用与原理
开发语言·后端·golang
bigear_码农5 小时前
python异步协程async调用过程图解
开发语言·python·线程·进程·协程
zhaoyqcsdn5 小时前
抽象工厂模式及其在自动驾驶中的应用举例(c++代码实现)
c++·经验分享·笔记·设计模式
天若有情6735 小时前
用 C++ 模拟 Axios 的 then 方法处理异步网络请求
网络·c++·php
知识分享小能手6 小时前
JavaScript学习教程,从入门到精通,Ajax与Node.js Web服务器开发全面指南(24)
开发语言·前端·javascript·学习·ajax·node.js·html5