Project Euler :Problem 54 Poker hands
来源:互联网 发布:炽焰帝国2激活码淘宝 编辑:程序博客网 时间:2024/06/16 09:34
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:
Highest card Nine
Highest card Seven
With Three Fours
with Three Threes
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?
果然不适合做动脑子的题目,完全没有耐心(╯‵□′)╯︵┻━┻
每轮每个人的牌都用两个数组来存,因为这题目比较的东西要么是牌值要么就是判断所有牌是不是同一个花色。
对于上面的是十个较项目,从下往上进行比较。
#include <iostream>#include <fstream>#include <string>#include <vector>#include <map>#include <algorithm>using namespace std;bool same_suit(char s[5]){if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3] && s[3] == s[4])return true;elsereturn false;}int cv(char a) {if (a >= '2'&&a <= '9')return a - '2';else if (a == 'T')return 8;else if (a == 'J')return 9;else if (a == 'Q')return 10;else if (a == 'K')return 11;elsereturn 12;}void ch_int(char a[5][2],int ac[5]){for (int i = 0; i < 5; i++){int tmp = cv(a[i][0]);ac[i] = tmp;}}int high_card(int ac[5],char s[5]){return ac[4];}int one_pair(int ac[5],char s[5]){int count = 0;for (int i = 5; i >= 1; i--){if (ac[i] == ac[i - 1])return ac[i];}return -1;}int two_pair(int ac[5],char s[5]){vector<int>res;int count = 0;for (int i = 1; i < 5; i++){if (ac[i] == ac[i - 1]){res.push_back(ac[i]);i++;}}if (res.size() == 2)return res[0] + res[1] * 13;elsereturn -1;}int three_kind(int ac[5],char s[5]){int count = 0;for (int i = 2; i < 5; i++){if (ac[i - 2] == ac[i - 1] && ac[i - 1] == ac[i])return ac[i - 2];}return -1;}int straight(int ac[5],char s[5]){if (ac[0] == 0 && ac[1] == 1 && ac[2] == 2 && ac[3] == 3 && ac[4] == 12)return ac[3];for (int i = 1; i < 5; i++){if (ac[i] != ac[i - 1] + 1)return -1;}return ac[4];}int flush(int ac[5],char s[5]){if (same_suit(s))return ac[4];return -1;}int full_house(int ac[5],char s[5]){if (ac[0] == ac[1] && ac[2] == ac[3] && ac[3] == ac[4])return ac[0] + ac[4] * 13;if (ac[0] == ac[1] && ac[1] == ac[2] && ac[3] == ac[4])return ac[4] + ac[0] * 13;return -1;}int four_kind(int ac[5],char s[5]){if ((ac[0] == ac[1] && ac[1] == ac[2] && ac[2] == ac[3]) || (ac[1] == ac[2] && ac[2] == ac[3] && ac[3] == ac[4]))return ac[2];return -1;}int strai_flush(int ac[5],char s[5]){int tmp = straight(ac,s);if (same_suit(s) && tmp >= 0)return ac[4];return -1;}int royal_flush(int ac[5], char s[5]){int tmp = strai_flush(ac, s);if (tmp >= 0 && ac[4] == 12)return ac[4];return -1;}int compareHighest(int ac[5], int bc[5]){for (int i = 4; i >= 0; i--){if (ac[i] > bc[i])return 1;if (ac[i] < bc[i])return 2;}}int comp(int ac[5], int bc[5], char as[5], char bs[5]){int(*compareList[10])(int *, char *) = { high_card, one_pair, two_pair, three_kind, straight, flush, full_house, four_kind, strai_flush, royal_flush };for (int i = 9; i >= 0; i--){int pa = (*compareList[i])(ac, as);int pb = (*compareList[i])(bc, bs);if (pa != -1 || pb != -1){if (pa == -1)return 2;if (pb == -1)return 1;if (pa > pb)return 1;if (pa < pb)return 2;if (pa == pb)return compareHighest(ac, bc);}}}int main(){ifstream input;input.open("poker.txt");string s;int ct = 0;while (getline(input, s)){char a[5][2];char b[5][2];int count = 0;int flag = 0;for (int i = 0; i < s.length(); i++){if (s[i] == ' '){count++;if (count>4){if (flag == 0)flag = 1;count -= 5;}}else{if (flag == 0){a[count][0] = s[i++];a[count][1] = s[i];}else{b[count][0] = s[i++];b[count][1] = s[i];}}}int ac[5], bc[5];char as[5], bs[5];for (int i = 0; i < 5; i++){a[i][1] = as[i];b[i][1] = bs[i];}ch_int(a, ac);ch_int(b, bc);sort(ac, ac + 5);sort(bc, bc + 5);if (comp(ac, bc, as, bs) == 1)ct++;}cout << ct << endl;system("pause");return 0;}
然后学C++都好几年了才知道有函数指针数组这种神奇好用的东东,感动哭。
- Project Euler :Problem 54 Poker hands
- Project Euler 54 Poker hands(模拟+hash)
- Project Euler problem 54
- UVa Problem Solution: 10315 - Poker Hands
- Euler Project Problem 6
- project euler problem 11
- Project Euler Problem 81
- Project Euler Problem 60
- Project Euler Problem 59
- Project Euler Problem 58
- Project Euler Problem 57
- Project Euler Problem 16
- Project Euler Problem 30
- Project Euler Problem 31
- Project Euler Problem 32
- Project Euler Problem 14
- Project Euler Problem 15
- Project Euler Problem 17
- jsp页面动态获取时间
- 2012年5月SAT香港真题解析
- 一些sql面试题
- 输入url到页面返回的全过程
- 构造函数和析构函数的使用
- Project Euler :Problem 54 Poker hands
- 网页中引用优酷播放器并使其被遮罩层遮住
- Android自定义消息推送
- iOS开发笔记--多手势事件
- 关于UGUI RectTransform的几个参数的获取和修改(待补充)
- linux c编程中调用 shell 命令
- mongodb aggregate 级联查询
- Hibernate annotation配置方式的MappedBy使用详解
- 65Valid Number