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

相关推荐
我爱学嵌入式18 分钟前
C语言第 9 天学习笔记:数组(二维数组与字符数组)
c语言·笔记·学习
hqxstudying21 分钟前
Java异常处理
java·开发语言·安全·异常
小坏坏的大世界2 小时前
C++ STL常用容器总结(vector, deque, list, map, set)
c++·算法
wjs20243 小时前
状态模式(State Pattern)
开发语言
我命由我123453 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
liulilittle3 小时前
C++ TAP(基于任务的异步编程模式)
服务器·开发语言·网络·c++·分布式·任务·tap
励志要当大牛的小白菜5 小时前
ART配对软件使用
开发语言·c++·qt·算法
PAK向日葵6 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
爱装代码的小瓶子7 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
Maybe_ch8 小时前
.NET-键控服务依赖注入
开发语言·c#·.net