1012. The Best Rank (25)

本文介绍了一个学生排名系统的实现过程,该系统能够根据学生的各科成绩(C语言、数学、英语及总平均分)进行排序,并处理同分情况下的排名逻辑。通过重写比较函数并利用C++标准库中的排序算法,确保了排名的准确性。

1.首先是平均分A,再到C,M,E

2.通过重写不同的cmp函数,使用algorithm.h里面的sort进行排序

3.如果分数相同,则排名相同,例如90,80,80,80,70,则排名为1,2,2,2,5


//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
struct student{
	int c, m, e, rank;
	int a;
	string course, ID;
	int tmpRank;//用来存在不同状态下的排名,主要是更新并列排名
	student() :c(0), m(0), e(0), a(0), rank(9999999), tmpRank(9999999), course(""), ID(""){};
};
bool cmpC(const student&a, const student&b)
{
	if (a.c > b.c) return true;
	else return false;
}
bool cmpM(const student&a, const student&b)
{
	if (a.m > b.m) return true;
	else return false;
}
bool cmpE(const student&a, const student&b)
{
	if (a.e > b.e) return true;
	else return false;
}
bool cmpA(const student&a, const student&b)
{
	if (a.a > b.a) return true;
	else return false;
}
int main(void) {

	int n, m;
	cin >> n >> m;
	vector<student> stu(n);
	for (int i = 0; i < n; i++)
	{
		cin >> stu[i].ID >> stu[i].c >> stu[i].m >> stu[i].e;
		stu[i].a = (stu[i].c + stu[i].m + stu[i].e);
		stu[i].course = "";
		stu[i].rank = 9999999;
	}
	//比较平均分
	sort(stu.begin(), stu.end(), cmpA);
	for (int i = 0; i < n; i++)
	{
		if (i == 0)
			stu[i].tmpRank = 0;
		else if (stu[i].a == stu[i - 1].a)
			stu[i].tmpRank = stu[i - 1].tmpRank;//如果分数相同,则排名相同
		else
			stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4
		if (stu[i].rank > stu[i].tmpRank + 1)
		{
			stu[i].rank = stu[i].tmpRank + 1;
			stu[i].course = "A";
		}
	}
	//比较C语言
	sort(stu.begin(), stu.end(), cmpC);
	for (int i = 0; i < n; i++)
	{
		if (i == 0)
			stu[i].tmpRank = 0;
		else if (stu[i].c == stu[i - 1].c)
			stu[i].tmpRank = stu[i - 1].tmpRank;//如果分数相同,则排名相同
		else
			stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4
		if (stu[i].rank > stu[i].tmpRank + 1)
		{
			stu[i].rank = stu[i].tmpRank + 1;
			stu[i].course = "C";
		}
	}
	//比较Mathematics
	sort(stu.begin(), stu.end(), cmpM);
	for (int i = 0; i < n; i++)
	{
		if (i == 0)
			stu[i].tmpRank = 0;
		else if (stu[i].m == stu[i - 1].m)
			stu[i].tmpRank = stu[i - 1].tmpRank;//如果分数相同,则排名相同
		else
			stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4
		if (stu[i].rank > stu[i].tmpRank + 1)
		{
			stu[i].rank = stu[i].tmpRank + 1;
			stu[i].course = "M";
		}
	}
	//比较English
	sort(stu.begin(), stu.end(), cmpE);
	for (int i = 0; i < n; i++)
	{
		if (i == 0)
			stu[i].tmpRank = 0;
		else if (stu[i].e == stu[i - 1].e)
			stu[i].tmpRank = stu[i - 1].tmpRank;//如果分数相同,则排名相同
		else
			stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4
		if (stu[i].rank > stu[i].tmpRank + 1)
		{
			stu[i].rank = stu[i].tmpRank + 1;
			stu[i].course = "E";
		}
	}
	map<string, student> ma;
	for (int i = 0; i < n; i++)
	{
		ma[stu[i].ID] = stu[i];
	}

	for (int i = 0; i < m; i++)
	{
		string tmp;
		cin >> tmp;
		if (ma.find(tmp) == ma.end())
			cout << "N/A" << endl;
		else
			cout << ma[tmp].rank << " " << ma[tmp].course << endl;
	}
	return 0;
}


To determine the number of distinct sets of 13 cards for which Player A can guarantee a win in a poker game where each player selects 5 cards and Player B wins on ties, the problem revolves around combinatorics and game theory. ### Problem Interpretation: - A standard deck has 52 cards. - Player A selects 13 cards as their hand. - From these 13 cards, Player A must be able to choose a 5-card poker hand that beats any 5-card poker hand that Player B could form from the remaining 39 cards. - If both players have the same rank of hand (e.g., both have a flush), Player B wins by default. - The objective is to count how many such 13-card combinations exist where Player A can always select a 5-card hand strictly better than any 5-card hand Player B could choose. ### Key Observations: - There are $\binom{52}{13}$ total possible 13-card hands. - Not all of these hands will allow Player A to guarantee a win. - The solution involves evaluating the strength of all possible 5-card combinations from the 13-card set and ensuring that none of the 5-card combinations from the remaining 39 cards can tie or beat them. ### Strategy to Solve: 1. **Understand Poker Hand Rankings**: The ranking of poker hands from highest to lowest is: Royal Flush, Straight Flush, Four of a Kind, Full House, Flush, Straight, Three of a Kind, Two Pair, One Pair, High Card. 2. **Generate All 13-Card Hands**: The number of ways to choose 13 cards from 52 is $\binom{52}{13}$, which is approximately $6.35 \times 10^{11}$. 3. **Evaluate Each 13-Card Hand**: - For each 13-card hand, generate all $\binom{13}{5} = 1287$ possible 5-card combinations. - Determine the best possible 5-card hand from these combinations. - From the remaining 39 cards, generate all $\binom{39}{5} = 575757$ possible 5-card combinations for Player B. - Check if any of Player B’s hands can tie or beat Player A’s best hand. 4. **Count Winning Hands for Player A**: - If Player A’s best 5-card hand beats all possible 5-card hands from the remaining 39 cards, count that 13-card set as a winning hand. - This requires a poker hand evaluator to compare the strength of hands. 5. **Implement Efficient Comparison**: - Use a precomputed lookup table for poker hand strengths. - Implement a fast comparison algorithm to avoid redundant computations. - Parallelize the computation to handle the large search space. 6. **Optimize with Pruning**: - Discard 13-card hands early if their best 5-card hand cannot beat the minimum possible strength of Player B’s hands. - For example, if Player A’s best hand is a high card, and Player B can form a pair, discard that 13-card set. 7. **Final Count**: - After evaluating all possible 13-card combinations and filtering out those where Player A cannot guarantee a win, the remaining count is the desired answer. ### Computational Complexity: This problem is computationally intensive due to the large number of combinations. It is not feasible to compute manually and would require: - Efficient poker hand evaluation code. - Distributed computing or GPU acceleration. - Optimization techniques such as memoization and pruning. Here is a simplified version of the code in Python: ```python from itertools import combinations import random # Simplified poker hand evaluator (placeholder) def evaluate_hand(hand): # This would be replaced with a full poker hand evaluator return random.randint(1, 1000000) # Generate a deck of 52 cards deck = list(range(52)) # Placeholder for counting winning hands winning_hands_count = 0 # Iterate over all possible 13-card hands (this is computationally infeasible to complete in full) for hand_A in combinations(deck, 13): remaining_cards = list(set(deck) - set(hand_A)) best_hand_A = max(combinations(hand_A, 5), key=evaluate_hand) # Check if best_hand_A beats all possible 5-card hands from the remaining 39 cards player_B_hands = combinations(remaining_cards, 5) if all(evaluate_hand(best_hand_A) > evaluate_hand(hand_B) for hand_B in player_B_hands): winning_hands_count += 1 print(f"Number of winning 13-card hands: {winning_hands_count}") ``` ### Conclusion: The exact number of such 13-card hands is not trivial to compute and would require a dedicated program with optimized algorithms. However, the framework for solving the problem involves evaluating all possible combinations and comparing poker hand strengths.
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值