UVA 131 - The Psychic Poker Player

本文深入探讨了信息技术领域的核心技术,包括但不限于前端开发、后端开发、移动开发、游戏开发、大数据开发、AI音视频处理等细分领域。文章详细介绍了各领域内的关键技术和实践,旨在帮助开发者深入了解并掌握信息技术领域的最新趋势和发展。

 

 

In 5-card draw poker, a player is dealt a hand of five cards (which may be looked at). The player may then discard between zero and five of his or her cards and have them replaced by the same number of cards from the top of the deck (which is face down). The object is to maximize the value of the final hand. The different values of hands in poker are given at the end of this problem.

Normally the player cannot see the cards in the deck and so must use probability to decide which cards to discard. In this problem, we imagine that the poker player is psychic and knows which cards are on top of the deck. Write a program which advises the player which cards to discard so as to maximize the value of the resulting hand.

 

Input and Output

Input will consist of a series of lines, each containing the initial five cards in the hand then the first five cards on top of the deck. Each card is represented as a two-character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades). Cards will be separated by single spaces. Each input line will be from a single valid deck, that is there will be no duplicate cards in each hand and deck.

Each line of input should produce one line of output, consisting of the initial hand, the top five cards on the deck, and the best value of hand that is possible. Input is terminated by end of file.

 

Use the sample input and output as a guide. Note that the order of the cards in the player's hand is irrelevant, but the order of the cards in the deck is important because the discarded cards must be replaced from the top of the deck. Also note that examples of all types of hands appear in the sample output, with the hands shown in decreasing order of value.

 

Sample Input

 

TH JH QC QD QS QH KH AH 2S 6S
2H 2S 3H 3S 3C 2D 3D 6C 9C TH
2H 2S 3H 3S 3C 2D 9C 3D 6C TH
2H AD 5H AC 7H AH 6H 9H 4H 3C
AC 2D 9C 3S KD 5S 4D KS AS 4C
KS AH 2H 3C 4H KC 2C TC 2D AS
AH 2C 9S AD 3C QH KS JS JD KD
6C 9C 8C 2D 7C 2H TC 4C 9S AH
3D 5S 2H QD TD 6S KH 9H AD QH

 

Sample Output

 

Hand: TH JH QC QD QS Deck: QH KH AH 2S 6S Best hand: straight-flush
Hand: 2H 2S 3H 3S 3C Deck: 2D 3D 6C 9C TH Best hand: four-of-a-kind
Hand: 2H 2S 3H 3S 3C Deck: 2D 9C 3D 6C TH Best hand: full-house
Hand: 2H AD 5H AC 7H Deck: AH 6H 9H 4H 3C Best hand: flush
Hand: AC 2D 9C 3S KD Deck: 5S 4D KS AS 4C Best hand: straight
Hand: KS AH 2H 3C 4H Deck: KC 2C TC 2D AS Best hand: three-of-a-kind
Hand: AH 2C 9S AD 3C Deck: QH KS JS JD KD Best hand: two-pairs
Hand: 6C 9C 8C 2D 7C Deck: 2H TC 4C 9S AH Best hand: one-pair
Hand: 3D 5S 2H QD TD Deck: 6S KH 9H AD QH Best hand: highest-card

 

 

 

下面列出从大到小的扑克牌面. 这是所有的扑克通用规则。

1
Royal Flush 同花大顺又称皇家同花顺 它是所有德州扑克中的王牌,即使您经常玩扑克,也很少见到这样的牌。好比打高尔夫球一杆进洞一样。它是由T(10)到Ace的清一色同花组成。Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
2
Straight Flush 同花顺 除了由最大同花所组成的同花大顺以外的同花组成的顺子。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
3
Four-of-a-Kind 四条 四张同样的牌+任意一张牌 。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
4
 

Full House 俘虏或船牌或葫芦 三条带一对,即三张同样的牌带两张同样的牌。如都是Full House,则先比较谁的三条大,如三条一样大,则比谁的两对大。如:

Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
5
Flush 五张同花 用五张同一花色但不相连的牌型组成,如都是五张同花,则谁的同花牌大谁赢。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
6
Straight 五张顺子 由五张相连但不同花色的牌组成,在连牌中,Ace是既可作最大也可以作最小的牌。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
7
Three-of-a-Kind 三条 即三张同样的牌。它有两种叫法,取决于一对牌是在您手中还是在桌上。一对在手中,桌上有一张,称之为“set”;v如手中有一张,桌上有一对,则称之为“Three of A Kind”。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
8
Two Pair 两对 由五张牌中的两对牌组成。如果都有两对,则先比大对,再比小对 。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
9
One Pair 一对 当不止一人有同样的一对牌时,则要比一对后面的牌,称之为“Kickers”。记住,德州扑克是挑选最好的五张牌去比。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player
   
10
High Card 大牌 无以上任何牌型时,决定牌的大小 。 Uva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>PlayerUva <wbr> <wbr>131 <wbr>- <wbr>The <wbr>Psychic <wbr>Poker <wbr>Player

 

 

 

 

可以用0-2^5 32个二进制数生成所有弃牌情况用0,1分别代表第x张牌弃还是留,然后从牌堆补到5张;

至于比大小,其实只有三大类,顺子(10 J Q K A特殊),同花,对子(包含多张相同);

统计对子可以将每次产生的五张牌分别统计与每一张相同的张数(包含自己)然后排序

 

 

#define RUN
#ifdef RUN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype> 
#include <algorithm>
#include <utility>
#include <math.h>
#include <ctime>

using namespace std;

#define MAXN 110

//输入序列
char numIn[11];	//牌的大小,标示值,如T,J,Q,K,A
int num[11];	//牌的大小,真实值
char suit[11];	//花色

int make[6];	//存放产生的二进制数

char name[10][MAXN] = {"highest-card","one-pair","two-pairs","three-of-a-kind","straight",
	"flush","full-house","four-of-a-kind","straight-flush"};

//手中牌的大小和花色
int Num[6];
int Suit[6];

int Win;

void printout(){
	for(int i=1; i<=10; i++){
		printf("%c%c ", numIn[i], suit[i]);
	}
	printf("\n");
	
	for(int i=1; i<=10; i++){
		printf("%d%c ", num[i], suit[i]);
	}
	printf("\n");
	printf("\n");
}

void printout2(){
	for(int i=1; i<=5; i++){
		printf("%d", make[i]);
	}
	printf("\n");
}

void printout3(){
	for(int i=1; i<=5; i++){
		printf("%d%c ", Num[i], Suit[i]);
	}
	printf("\n");
}

//判断牌大小和花色来确定什么牌
void play(){
	int win=0, shunzi=1, tonghua=1;
	int sum[6];	//存放重复的个数

	memset(sum, 0, sizeof(sum));

	//判断是否顺子
	for(int i=1; i<5; i++){
		if(Num[i]+1 != Num[i+1]){
			shunzi = 0;
			break;
		}
	}

	//顺子的特殊情况:(A,10,J,Q,K)
	if(Num[1]==1 && Num[2]==10 && Num[3]==11 && Num[4]==12 && Num[5]==13){
		shunzi = 1;
	}

	//判断是否同花
	for(int i=1; i<5; i++){
		if(Suit[i] != Suit[5]){
			tonghua = 0;
		}
	}

	//准备计算对子的数目
	//牌面大小为i共有sum[i]张
	for(int i=1; i<=5; i++){
		for(int j=1; j<=5; j++){
			if(Num[i] == Num[j]){
				sum[i]++;
			}
		}
	}

	//升序冒泡排序sum数组
	for(int i=1; i<5; i++){
		for(int j=i+1; j<=5; j++){
			if(sum[i] > sum[j]){
				int t = sum[i];
				sum[i] = sum[j];
				sum[j] = t;
			}
		}
	}

	//根据定义来确定最佳的牌面
	//对子 one-pair
	if(sum[5] == 2){
		win = 1;
	}

	//两对 two-pairs
	if(sum[2]+sum[3] == 4){
		win = 2;
	}

	//三条 three-of-a-kind
	if(sum[5] == 3){
		win = 3;
	}

	//顺子 straight
	if(shunzi){
		win = 4;
	}

	//五张同花 flush
	if(tonghua){
		win = 5;
	}

	//三条且一对 full-house
	if(sum[2]+sum[3] == 5){
		win = 6;
	}

	//4张同牌 four-of-a-kind
	if(sum[2] == 4){
		win = 7;
	}

	//同花顺 straight-flush
	if(tonghua+shunzi == 2){
		win = 8;
	}

	if(win > Win){
		Win = win;
	}
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("131.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	
	while(scanf("%c%c", &numIn[1],&suit[1]) != EOF){
		for(int i=2; i<=10; i++){
			scanf(" %c%c", &numIn[i], &suit[i]);
		}
		getchar();
		
		Win = 0;

		//把牌值的T,J,Q,K,A转为数字,存入num
		for(int i=1; i<=10; i++){
			if(numIn[i] == 'T'){
				num[i] = 10;
			}
			else if(numIn[i] == 'J'){
				num[i] = 11;
			}
			else if(numIn[i] == 'Q'){
				num[i] = 12;
			}
			else if(numIn[i] == 'K'){
				num[i] = 13;
			}
			else if(numIn[i] == 'A'){
				num[i] = 1;
			}
			else if(isdigit(numIn[i])){
				num[i] = numIn[i] - '0';
			}
		}

		//printout();

		//产生5位二进制数的所有可能,从0到2^5-1
		for(int i=0; i<(1<<5); i++){
			int binDigit = 5;
			int tmp = i;
			memset(make, 0, sizeof(make));
			while(tmp != 0){
				make[binDigit] = tmp % 2;
				tmp /= 2;
				binDigit--;
			}

			//printout2();

			//根据产生的二进制序列来选择该丢弃哪一张牌,序列中0表示抛弃,1表示保留
			int dumpCnt = 0;
			int dump[6];
			memset(dump, 0, sizeof(dump));
			for(int i=1; i<=5; i++){
				if(make[i] == 0){
					dumpCnt++;
					//摸最顶端的牌来填补到前面被抛弃牌的位置
					Num[i] = num[5+dumpCnt];
					Suit[i] = suit[5+dumpCnt];
				}
				else{
					//没被抛弃的牌保持不变
					Num[i] = num[i];
					Suit[i] = suit[i];
				}
			}
			
			//printout3();

			//从小到大冒泡排序
			for(int i=1; i<5; i++){
				for(int j=i+1; j<=5; j++){
					if(Num[i] > Num[j]){
						int t1 = Num[i];
						Num[i] = Num[j];
						Num[j] = t1;
						char t2 = Suit[i];
						Suit[i] = Suit[j];
						Suit[j] = t2;
					}
				}
			}

			//printout3();

			play();

			


		}//End of generating binary

		

		printf("Hand: ");
		for(int i=1; i<=5; i++){
			printf("%c%c ", numIn[i], suit[i]);
		}

		printf("Deck: ");
		for(int i=6; i<=10; i++){
			printf("%c%c ", numIn[i], suit[i]);
		}

		printf("Best hand: %s\n", name[Win]);

	}

}


#endif

 

 

 

内容概要:本文详细介绍了一个基于Java和Vue的联邦学习隐私保护推荐系统的设计与实现。系统采用联邦学习架构,使用户数据在本地完成模型训练,仅上传加密后的模型参数或梯度,通过中心服务器进行联邦平均聚合,从而实现数据隐私保护与协同建模的双重目标。项目涵盖完整的系统架构设计,包括本地模型训练、中心参数聚合、安全通信、前后端解耦、推荐算法插件化等模块,并结合差分隐私与同态加密等技术强化安全性。同时,系统通过Vue前端实现用户行为采集与个性化推荐展示,Java后端支撑高并发服务与日志处理,形成“本地训练—参数上传—全局聚合—模型下发—个性化微调”的完整闭环。文中还提供了关键模块的代码示例,如特征提取、模型聚合、加密上传等,增强了项目的可实施性与工程参考价值。 适合人群:具备一定Java和Vue开发基础,熟悉Spring Boot、RESTful API、分布式系统或机器学习相关技术,从事推荐系统、隐私计算或全栈开发方向的研发人员。 使用场景及目标:①学习联邦学习在推荐系统中的工程落地方法;②掌握隐私保护机制(如加密传输、差分隐私)与模型聚合技术的集成;③构建高安全、可扩展的分布式推荐系统原型;④实现前后端协同的个性化推荐闭环系统。 阅读建议:建议结合代码示例深入理解联邦学习流程,重点关注本地训练与全局聚合的协同逻辑,同时可基于项目架构进行算法替换与功能扩展,适用于科研验证与工业级系统原型开发。
源码来自:https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值