文章目錄

原题链接 http://projecteuler.net/problem=54

Poker hands

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

  • High Card: Highest value card.
  • One Pair: Two cards of the same value.
  • Two Pairs: Two different pairs.
  • Three of a Kind: Three cards of the same value.
  • Straight: All cards are consecutive values.
  • Flush: All cards of the same suit.
  • Full House: Three of a kind and a pair.
  • Four of a Kind: Four cards of the same value.
  • Straight Flush: All cards are consecutive values of same suit.
  • Royal Flush:Ten, Jack, Queen, King, Ace, in same suit.
    The cards are valued in the order:
    2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.

Consider the following five hands dealt to two players:


























































HandPlayer 1Player 2Winner
15H 5C 6S 7S KD
Pair of Fives
2C 3S 8S 8D TD
Pair of Eights
Player 2
25D 8C 9S JS AC
Highest card Ace
2C 5C 7D 8S QH
Highest card Queen
Player 1
32D 9C AS AH AC
Three Aces
3D 6D 7D TD QD
Flush with Diamonds
Player 2
44D 6S 9H QH QC
Pair of Queens
Highest card Nine
3D 6D 7H QD QS
Pair of Queens
Highest card Seven
Player 1
52H 2D 4C 4D 4S
Full House
With Three Fours
3C 3D 3S 9S 9D
Full House
with Three Threes
Player 1

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1’s cards and the last five are Player 2’s cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player’s hand is in no specific order, and in each hand there is a clear winner.

How many hands does Player 1 win?

扑克手牌

在扑克牌游戏中,一手牌由5张牌构成,有不同的等级,从最小到最大,规则如下:

大牌:牌中最大的

对子:两张相同的牌

两对:有两对对子

三条:三张相同的牌

顺子:所有的牌是连续的

同花:所有的牌是同一花色

葫芦:三条和一对

四条:四张相同的牌

同花顺:所有的牌连续且是同花

同花大顺:AKQJ10组成的同花顺

牌值的顺序如下:

2,3,4,5,6,7,8,9,10,J,Q,K,A

如果两个玩家有相同等级的牌,则值更大的那个赢,例如一对8赢一对5(如下例1),但是如果等级相同,例如两个玩家都有一对Q,那么将比较手牌中的最大值(如下例4),如果最大的依然相同,则比较次大的,重复如上步骤。

考虑如下5种两个玩家的对局情况:

玩家1

玩家2

赢家

1

5H 5C 6S 7S KD
一对5

2C 3S 8S 8D TD
一对8

玩家2

2

5D 8C 9S JS AC
最大牌A

2C 5C 7D 8S QH
最大牌Q

玩家1

3

2D 9C AS AH AC
3张A

3D 6D 7D TD QD
方块同花

玩家2

4

4D 6S 9H QH QC
一对Q
最大9

3D 6D 7H QD QS
一对Q
最大7

玩家1

5

2H 2D 4C 4D 4S
葫芦
三个四

3C 3D 3S 9S 9D
葫芦
三个三

玩家1




文件 poker.txt中包含一千次两个玩家的对局情况,每一行有10张牌(空格分开),前5张牌是玩家1的牌,后5张牌是玩家2的牌,你可以假设手牌都是有效的(没有无效字符和重复的牌),每个玩家的手牌都没有特殊顺序,并且每一对局一定有一个赢。

求玩家1共赢了多少次。

解答:

这题好麻烦啊,写的代码真是乱。就是按照扑克牌的规则比较大小,真是很麻烦,一度令我有放弃欧拉工程的冲动,还好坚持下来了。

打赏作者

文章目錄