欧拉工程第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都包含同样的数字. 这个题目相对比较简单 暴 ...
随机推荐
- 53.转:深入浅出FPGA-14-ChipScope软件使用
引言 索性再破例一下,成个系列也行. 内容组织 1.建立工程 2.插入及配置核 2.1运行Synthesize 2.2新建cdc文件 2.3 ILA核的配置 3. Implement and gene ...
- 11.Warning (332060): Node: pi_fck3p was determined to be a clock but was found without an associated clock assignment.
解释及措施:(1):这个信号是不是你期望的时钟信号?还是被综合器误将普通信号综合成了时钟信号?有没有在代码中用过这个信号的上升沿/下降沿? (2):如果是期望的时钟信号,那么是否有可能调整管脚位置约束 ...
- 28335 sci fifo send
#include "DSP2833x_Device.h"#include "DSP2833x_Examples.h"char buf[]={0x30,0x32, ...
- Asp.net操作Excel(终极方法NPOI)(转)
原文:Asp.net操作Excel(终极方法NPOI) 先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中 ...
- 查询sql耗时(运行时间)
SET STATISTICS TIME ON SELECT * FROM dbo.UserBase SET STATISTICS TIME OFF
- ASP.NET MVC 4 插件化架构简单实现-思路篇
用过和做过插件的都会了解插件的好处,园子里也有很多和讨论,但大都只些简单的加载程序集什么的,这里主要讨论的就是使用 ASP.NET MVC 4 来实现每个插件都可以完全从主站点剥离出来,即使只是一个插 ...
- Eclipse支持Jquery代码提示(JqeuryWTP)
问题描述: Eclipse支持Jquery代码提示 问题解决: 下载 JqueryWTP.jar文件 文件替换 在Eclipse/plugin 路径下, ...
- poj 1386 Play on Words 有向欧拉回路
题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...
- webservice之XFire的使用(java调用java)
注意:xfire不支持java.util.List等集合,所以调用webservice传递的参数要为基本类型. 转自:http://zp9245.blog.163.com/blog/static/10 ...
- [百度空间] [转]DLL地狱及其解决方案
DLL地狱及其解决方案 原作者:Ivan S Zapreev 译者:陆其明概要 本文将要介绍DLL的向后兼容性问题,也就是著名的“DLL Hell”问题.首先我会列出自己的研究结果,其中包括其它一些研 ...