欧拉工程第54题:Poker hands
package projecteuler51to60; import java.awt.peer.SystemTrayPeer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.TreeSet; class Card{
//一张一张的读取,一张牌,以及对于的颜色
public final int rank;
public final int suit;
public Card(int rank,int suit){
if(rank<0 || rank>= 13 ||suit< 0||suit>= 4)
throw new IllegalAccessError();
this.rank= rank;
this.suit= suit;
}
public Card(String str){
this("23456789TJQKA".indexOf(str.charAt(0)), "SHCD".indexOf(str.charAt(1)));
} public boolean equals(Object obj) {
if (!(obj instanceof Card))
return false;
Card other = (Card)obj;
return rank == other.rank && suit == other.suit;
} public int hashCode() {
return rank * 4 + suit;
} } class p54{
void solve0(){
String filenameString="src//projecteuler51to60//p054_poker.txt";
readTxtFile(filenameString);
}
int Judge(String[] twoManCard){
Card[] player1 = new Card[5];
Card[] player2 = new Card[5]; for(int i=0;i<5;i++){
player1[i] = new Card(twoManCard[i+0]);
player2[i] = new Card(twoManCard[i+5]);
}
if (getScore(player1) > getScore(player2))
return 1;
return 0;
}
int getScore(Card[] hand){
if(hand.length !=5)
throw new IllegalArgumentException();
int[] rankCounts = new int[13];
int flushSuit = hand[0].suit;
//长度是13的数组,数组值是:各位置数出现的次数
//flushSuit 判断颜色是否一样
for(Card card:hand){
rankCounts[card.rank]++;
if(card.suit!=flushSuit)
flushSuit = -1;
}
//rankCountHist 统计rankCounts中出现了多少次 0 1 2 3 4 5,rankCounts中所有数之和是5,应该就五张牌
int[] rankCountHist = new int[6];
for(int i=0;i<rankCounts.length;i++)
rankCountHist[rankCounts[i]]++;
int bestCards = get5FrequnetHighestCards(rankCounts, rankCountHist);
int straightHishRank = getStraighHighRank(rankCounts);
if(flushSuit!=-1)System.out.println("ok");
// Straight flush
if(straightHishRank!=-1 && flushSuit!=-1) return 8<<20|straightHishRank;
// Four of a kind
else if(rankCountHist[4]==1) return 7<<20|bestCards;
// Full house
else if(rankCountHist[3]==1&&rankCountHist[2]==1)return 6<<20|bestCards;
// Flush
else if(flushSuit!=-1) return 5<<20|bestCards;
// Straight
else if(straightHishRank!=-1) return 4<<20|straightHishRank;
// Three of a kind
else if(rankCountHist[3]==1) return 3<<20|bestCards;
// Two pairs
else if(rankCountHist[2]==2) return 2<<20|bestCards;
// One pair
else if(rankCountHist[2]==1) return 1<<20|bestCards;
// High card
else return 0<<20|bestCards;
}
int get5FrequnetHighestCards(int[] ranks,int[] ranksHist) {
int result = 0;
int count =0;
for(int i=ranksHist.length-1;i>=0;i--){
for(int j=ranks.length-1;j>=0;j--){
if(ranks[j]==i){
for(int k=0;k<i&&count<5;k++,count++)
result=result<<4|j;
}
}
} return result;
}
int getStraighHighRank(int[] ranks){
outer:
for(int i=ranks.length -1;i>=3;i--){
for(int j=0;j<5;j++){
if(ranks[(i-j+13)%13] ==0)
continue outer;
}
return i;
}
return -1;
} void readTxtFile(String filename){
File fl =new File(filename);
int res=0;
FileReader reader=null;
try {
reader = new FileReader(fl);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} BufferedReader br = new BufferedReader(reader);
String line="";
try {
while((line=br.readLine())!=null){
String[] twoManCard = line.split(" ");
res+=Judge(twoManCard); }
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("no file");
}
System.out.println(res);
} }
public class Problem54 { public static void main(String[] args){
long begin= System.currentTimeMillis();
new p54().solve0();
long end = System.currentTimeMillis();
long Time = end - begin;
System.out.println("Time:"+Time/1000+"s"+Time%1000+"ms");
} }
欧拉工程第54题:Poker hands的更多相关文章
- 欧拉工程第69题:Totient maximum
题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- 欧拉工程第67题:Maximum path sum II
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- 欧拉工程第65题:Convergents of e
题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...
- 欧拉工程第56题:Powerful digit sum
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...
- 欧拉工程第55题:Lychrel numbers
package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...
- 欧拉工程第53题:Combinatoric selections
package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...
- 欧拉工程第52题:Permuted multiples
题目链接 题目: 125874和它的二倍,251748, 包含着同样的数字,只是顺序不同. 找出最小的正整数x,使得 2x, 3x, 4x, 5x, 和6x都包含同样的数字. 这个题目相对比较简单 暴 ...
随机推荐
- 38.基于FPGA的FIR设计二
利用fdatool工具生成的滤波器系数与用代码生成的系数不一致,在网上查询得知,fdatool生成的滤波器系数是有符号小数,而且是浮点型,而代码生成的滤波器系数是定点型有符号数,故不一样. 浮点型数据 ...
- 使用C语言在Win控制台中输出带颜色的文字
学了这么久的C语言,一直停留在编写“控制台”程序的水平.黑色窗口,白色的文字,看多了着实让人感到枯燥无味.但是作为业余爱好者,我既没有那么多时间和精力去学习如何编写窗口程序,也没有那个必要一定用C去调 ...
- week 9 scenario testing
1:How do you expect different personas to use your software? What’s their need and their goals, ho ...
- [Z] 深入浅出 Systemd
1. Systemd 的简介和特点 Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度.systemd 和 u ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- git学习——<一>git安装
一.windows.linux平台安装 windows平台安装简单方便,到git官网上下载exe安装包即可,会把git bash shell给你安装好,你到命令窗口便可直接使用. linux平台安装, ...
- rivers ioi2005 树形dp
说句实话,写完这道题,很想吐一口血出来,以示我心情的糟糕: 题目很简单,树形dp,正常做30分钟,硬是做了好几个小时,真是伤心. 题解不写了,只是吐个槽,网上没有用背包写的dp,全是左儿子右兄弟写法, ...
- [百度空间] [原]MFC杂乱笔记
1. 创建动态菜单 假如ID是动态分配的,那么重载virtual BOOLOnCmdMsg(UINT,int,void*,AFX_CMDHANDLERINFO*); 据MSDN不详细解释,当第二个参数 ...
- URAL题解—不断跟新中
1014:简单题,忘了0的情况可以是10,== 1219:找呀找规律,满足N*(N-1)/2+1=X;就是1 的情况了
- #!--->hashbang技术
url中的#! URL 中的 # 本来的用途是跳转到页内锚点.一个 URL 中 # 后的值 (hash tag) 不影响所访问网页的内容,所以搜索引擎在处理仅仅 hash tag 不同的多个 URL ...