Project Euler :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:
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 |
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;
else
return 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;
else
return 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;
else
return -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:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- Project Euler:Problem 63 Powerful digit counts
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...
- Project Euler:Problem 86 Cuboid route
A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...
- Project Euler:Problem 76 Counting summations
It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...
- Project Euler:Problem 87 Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...
- Project Euler:Problem 89 Roman numerals
For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...
- Project Euler:Problem 93 Arithmetic expressions
By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...
- Project Euler:Problem 39 Integer right triangles
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...
- Project Euler:Problem 28 Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...
随机推荐
- 安卓实训第四天--基于HttpClient来完毕数据在server和设备间的交互。
上午:老师首先回想了昨天作业. 首先在安卓project中的TOOLS文件里,解析字节流那里,不用改变.而是把server端的编码方式变为UTF-8,然后将在安卓project的LoginActivi ...
- linux内核设计的艺术--系统启动第一步
计算机究竟是如何执行起来的呢,在我学习计算机的时候一直不是非常明确,可是近期借了本<linux内核设计的艺术>算是知道了计算机从按开机到启动操作系统之间究竟做了些什么. 这本书刚開始介绍的 ...
- 提高FPGA速度的quartus编译选项
Turning on some optimizations in Quartus II may help increase it. Here are some you may want to try: ...
- PermissionError: [Errno 13] in python
出现该错误,首先尝试以管理员身份运行 cmd.exe 程序,然后关闭所有的与 python 相关的进程. 1. open 打开一个文件夹(目录),而不是文件 这一错误一般发生在使用 open函数对文件 ...
- [学习笔记]node.js中的path.extname方法
path.extname 返回path路径文件扩展名,如果path以 ‘.' 为结尾,将返回 ‘.',如果无扩展名 又 不以'.'结尾,将返回空值. path.extname('index.html' ...
- 使用Chrome浏览器,让我们远离(所有)广告
你是否还在为浏览网页时各种广告霸屏而急躁不安?这里分享一个小技巧,如何自动屏蔽各大广告. 这里使用的浏览器是Chrome,直接在Chrome网上应用商店搜索下载安装AdBlock插件(不知道其它浏览器 ...
- MySQL格式化日期参数
MySQL格式化日期参数 %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) %e 月的天,数值(0-31) %f 微秒 %H 小时 (0 ...
- AngularJs轻松入门(三)MVC架构
MVC应用程序架构最早于1970年起源于Smalltalk语言,后来在桌面应用程序开发中使用较为广泛,如今在WEB开发中也非常流行.MVC的核心思想是將数据的管理(Model).业务逻辑控制(Cont ...
- BZOJ 3110 [Zjoi2013]K大数查询(整体二分)
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 11654 Solved: 3505[Submit][St ...
- webStrom的破解以及汉化
破解方法: 把JetbrainsCrack-3.1-release-enc.jar包放到bin目录下,然后把webstorm64.exe.vmoptions文件用文本打开, 在最后面加上一句-java ...