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

相关推荐
lkbhua莱克瓦2410 分钟前
用C语言实现了——一个基于顺序表的插入排序演示系统
c语言·开发语言·数据结构·程序人生·github·排序算法·交互
flying robot1 小时前
js在浏览器执行原理
开发语言·javascript·ecmascript
dhxhsgrx4 小时前
PYTHON训练营DAY25
java·开发语言·python
君鼎5 小时前
C++设计模式——单例模式
c++·单例模式·设计模式
风逸hhh7 小时前
python打卡day25@浙大疏锦行
开发语言·python
刚入门的大一新生7 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
小冯的编程学习之路7 小时前
【软件测试】:推荐一些接口与自动化测试学习练习网站(API测试与自动化学习全攻略)
c++·selenium·测试工具·jmeter·自动化·测试用例·postman
chxii8 小时前
5java集合框架
java·开发语言
老衲有点帅8 小时前
C#多线程Thread
开发语言·c#