[Project Euler] Problem 54
2012-06-26 11:59
330 查看
Problem Description
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:
Hand | Player 1 | Player 2 | Winner | |||
1 | 5H 5C 6S 7S KD Pair of Fives | 2C 3S 8S 8D TD Pair of Eights | Player 2 | |||
2 | 5D 8C 9S JS AC Highest card Ace | 2C 5C 7D 8S QH Highest card Queen | Player 1 | |||
3 | 2D 9C AS AH AC Three Aces | 3D 6D 7D TD QD Flush with Diamonds | Player 2 | |||
4 | 4D 6S 9H QH QC Pair of Queens Highest card Nine | 3D 6D 7H QD QS Pair of Queens Highest card Seven | Player 1 | |||
5 | 2H 2D 4C 4D 4S Full House With Three Fours | 3C 3D 3S 9S 9D Full House with Three Threes | Player 1 |
How many hands does Player 1 win?
C#
This solution is clear. Firstly, we parse poker string as some structure which stores both card value and suit.Then we calculate the score of each player’s hand, compare them, get the result.
The main codes:
public static void Run() { string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, POKER_FILE_NAME); string pokerString; using(StreamReader sr = new StreamReader(filePath)) { pokerString = sr.ReadToEnd(); } var handStrArray = pokerString.Split(new [] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); int player1WinCount = 0; foreach (var handStr in handStrArray) { HandCards player1 = new HandCards(); HandCards player2 = new HandCards(); GetPlayerHand(handStr, ref player1, ref player2); HandScore player1Score = player1.GetHandScore(); HandScore player2Score = player2.GetHandScore(); int result = player1Score.CompareTo(player2Score); if (result > 0) { player1WinCount++; } } Console.WriteLine(player1WinCount); }
相关文章推荐
- Project Euler :Problem 54 Poker hands
- Project Euler :Problem 54 Poker hands
- Project Euler:Problem 55 Lychrel numbers
- Project Euler:Problem 78 Coin partitions
- Project Euler__problem 1
- [Project Euler] Problem 53
- Project Euler:Problem 28 Number spiral diagonals
- Project Euler__problem 10
- Project Euler:Problem 39 Integer right triangles
- project euler 54
- 【Project Euler】【Problem 3】Largest prime factor
- Project Euler_Problem 26 欧拉计划26题
- Project Euler:Problem 86 Cuboid route
- Project Euler:Problem 1
- [Project Euler]加入欧拉 Problem 9
- [Project Euler]加入欧拉 Problem 15
- [Project Euler] Problem 9
- [Project Euler] Problem 23
- project Euler problem 8
- [Project Euler] Problem 20