决胜出比赛第一名

本文介绍了一种基于队列的数据结构实现的比赛排名算法。该算法通过给定的队伍实力对比关系和出场顺序,逐步模拟比赛过程,最终确定每个队伍的排名。适用于两两对决的淘汰赛制。

问题描述:

n支队伍比赛,分别编号为0,1,2。。。。n-1,已知它们之间的实力对比关系,存储在一个二维数组w[n][n]中,w[i][j] 的值代表编号为i,j的队伍中更强的一支。

所以w[i][j]=i 或者j,现在给出它们的出场顺序,并存储在数组order[n]中,比如order[n] = {4,3,5,8,1......},那么第一轮比赛就是 4对3, 5对8。.......
胜者晋级,败者淘汰,同一轮淘汰的所有队伍排名不再细分,即可以随便排,下一轮由上一轮的胜者按照顺序,再依次两两比,比如可能是4对5,直至出现第一名
编程实现,给出二维数组w,一维数组order 和 用于输出比赛名次的数组result[n],求出result。


算法:利用队列来实现,当队列中元素个数大于1时,则队首两元素出队PK,胜利的那个元素入队,直至队列中只剩一个元素,即为第一名。

代码实现:

#include <iostream>
#include <queue>

using namespace std;

#define TEAM_COUNT	7

void  getResult(int w[][TEAM_COUNT], int order[], int result[], int n);

int main(int, char **)
{
	//假设胜负关系:6 > 0 > 2 > 3 > 1 > 4 > 5
	int w[TEAM_COUNT][TEAM_COUNT] = {
		{0, 0, 0, 0, 0, 0, 6},
		{0, 1, 2, 3, 1, 1, 6},
		{0, 2, 2, 2, 2, 5, 6},
		{0, 3, 2, 3, 3, 3, 6},
		{0, 1, 2, 3, 4, 4, 6},
		{0, 1, 2, 3, 4, 5, 6},
		{6, 6, 6, 6, 6, 6, 6}
	};

	int oreder[TEAM_COUNT] = {0, 1, 2, 3, 4, 5, 6}; //出场顺序
	int result[TEAM_COUNT];							//排名

	getResult(w, oreder, result, TEAM_COUNT);

	//打印排名:注意题目要求,同一轮淘汰的所有队伍排名不再细分,即可以随便排
	cout << "排名如下:" << endl;
	for (int s = 0; s < TEAM_COUNT; s++)
		cout << result[s] << "  ";
	cout << endl;

	system("pause");
	return 0;
}

void getResult(int w[][TEAM_COUNT], int order[], int result[], int n)
{
	queue<int> qQueue;
	for (int i = 0; i < n; i++)
		qQueue.push(order[i]);

	int firstNo, secondNo;
	int winNo;
	while (qQueue.size() > 1)
	{
		firstNo = qQueue.front();
		qQueue.pop();
		secondNo = qQueue.front();
		qQueue.pop();
		winNo = w[firstNo][secondNo];
		qQueue.push(winNo);

		if (firstNo == w[firstNo][secondNo])
			result[secondNo] = n;
		else
			result[firstNo] = n;

		n--;
	}

	result[qQueue.front()] = 1;
}


好的,让我们开始设计一个用C语言实现的21点游戏吧! 首先,我们需要定义一些基本的规则: 1. 游戏玩家和电脑都会拿到两张牌,牌面点数加起来最接近21点的获胜; 2. 牌面点数从2到10的牌,点数就是牌面显示的数字; 3. J、Q、K的点数都为10点; 4. A牌可以算作1点或11点; 5. 游戏中不能超过21点,超过21点判定为输; 6. 玩家可以选择要牌或停牌,电脑也会自动发牌直到牌面点数大于等于17点。 有了基本规则后,我们就可以开始设计游戏流程了。 首先,我们需要定义一个结构体来表示一张牌: ```c struct card { char* face; // 牌面,如"2", "J", "Q", "K", "A" int value; // 牌点数,2-10表示2-10点,J、Q、K为10点,A为1或11点 }; ``` 然后,我们需要定义一个数组来存储所有的牌: ```c struct card deck[52] = { {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10}, {"J", 10}, {"Q", 10}, {"K", 10}, {"A", 11}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10}, {"J", 10}, {"Q", 10}, {"K", 10}, {"A", 11}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10}, {"J", 10}, {"Q", 10}, {"K", 10}, {"A", 11}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5}, {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10}, {"J", 10}, {"Q", 10}, {"K", 10}, {"A", 11} }; ``` 接下来,我们需要定义一个函数来随机抽取一张牌: ```c struct card draw_card() { int rand_index = rand() % 52; return deck[rand_index]; } ``` 然后,我们需要定义一个函数来计算手中牌的点数: ```c int calculate_points(struct card* hand, int num_cards) { int total_points = 0; int num_aces = 0; for (int i = 0; i < num_cards; i++) { total_points += hand[i].value; if (strcmp(hand[i].face, "A") == 0) { num_aces++; } } while (num_aces > 0 && total_points > 21) { total_points -= 10; num_aces--; } return total_points; } ``` 接下来,我们需要定义一个函数来判断输赢: ```c int determine_winner(int player_points, int dealer_points) { if (player_points > 21) { return -1; // 玩家输了 } else if (dealer_points > 21) { return 1; // 玩家赢了 } else if (player_points > dealer_points) { return 1; // 玩家赢了 } else if (player_points < dealer_points) { return -1; // 玩家输了 } else { return 0; // 平局 } } ``` 最后,我们需要定义一个主函数来控制游戏流程: ```c int main() { srand(time(NULL)); // 初始化随机数种子 struct card player_hand[10]; // 玩家手中的牌 struct card dealer_hand[10]; // 电脑手中的牌 int num_player_cards = 0; // 玩家手中牌的数量 int num_dealer_cards = 0; // 电脑手中牌的数量 int player_points = 0; // 玩家手中牌的点数 int dealer_points = 0; // 电脑手中牌的点数 // 发两张牌给玩家 player_hand[num_player_cards++] = draw_card(); player_hand[num_player_cards++] = draw_card(); // 发两张牌给电脑 dealer_hand[num_dealer_cards++] = draw_card(); dealer_hand[num_dealer_cards++] = draw_card(); // 显示玩家手中的牌和点数 printf("Your cards:\n"); for (int i = 0; i < num_player_cards; i++) { printf("%s ", player_hand[i].face); } printf("\n"); player_points = calculate_points(player_hand, num_player_cards); printf("Your points: %d\n", player_points); // 玩家选择要牌或停牌 while (player_points < 21) { char choice; printf("Do you want to hit (h) or stand (s)? "); scanf(" %c", &choice); if (choice == 'h') { player_hand[num_player_cards++] = draw_card(); printf("Your cards:\n"); for (int i = 0; i < num_player_cards; i++) { printf("%s ", player_hand[i].face); } printf("\n"); player_points = calculate_points(player_hand, num_player_cards); printf("Your points: %d\n", player_points); } else { break; } } // 玩家输赢判断 if (player_points > 21) { printf("Bust! You lose.\n"); return 0; } // 电脑自动发牌直到点数大于等于17点 while (dealer_points < 17) { dealer_hand[num_dealer_cards++] = draw_card(); dealer_points = calculate_points(dealer_hand, num_dealer_cards); } // 显示电脑手中的牌和点数 printf("Dealer's cards:\n"); for (int i = 0; i < num_dealer_cards; i++) { printf("%s ", dealer_hand[i].face); } printf("\n"); dealer_points = calculate_points(dealer_hand, num_dealer_cards); printf("Dealer's points: %d\n", dealer_points); // 判断输赢 int result = determine_winner(player_points, dealer_points); if (result == 1) { printf("You win!\n"); } else if (result == -1) { printf("You lose!\n"); } else { printf("It's a tie!\n"); } return 0; } ``` 这就是一个简单的21点游戏的C语言实现,希望能对你有所帮助!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值