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

相关推荐
承渊政道13 分钟前
【动态规划算法】(完全背包问题从状态定义到空间优化)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
超级大福宝15 分钟前
【力扣48. 旋转图像】超好记忆版 + 口诀
c++·算法·leetcode
lzh2004091918 分钟前
深入学习Linux进程间通信:共享内存
linux·c++
相醉为友28 分钟前
040 Linux/裸机/RTOS 项目开发的跨平台兼容性——C语言静态接口抽象底层原理分析
linux·c语言·mcu
特种加菲猫34 分钟前
多态:让代码拥有“千变万化”的能力
开发语言·c++
Mr_pyx39 分钟前
【LeetHOT100】LRU缓存——Java多解法详解
java·开发语言
zx28596340040 分钟前
Laravel 4.x:颠覆PHP框架的10大革新特性
开发语言·php·laravel
threelab1 小时前
Three.js 咖啡杯烟雾效果 | 三维可视化 / AI 提示词
开发语言·javascript·人工智能
莫等闲-1 小时前
代码随想录一刷记录Day44——leetcode1143.最长公共子序列 53. 最大子序和
数据结构·c++·算法·leetcode·动态规划