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的更多相关文章

  1. 欧拉工程第69题:Totient maximum

    题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...

  2. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  3. 欧拉工程第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 ...

  4. 欧拉工程第66题:Diophantine equation

    题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...

  5. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

  6. 欧拉工程第56题:Powerful digit sum

    题目链接   Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...

  7. 欧拉工程第55题:Lychrel numbers

    package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...

  8. 欧拉工程第53题:Combinatoric selections

    package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...

  9. 欧拉工程第52题:Permuted multiples

    题目链接 题目: 125874和它的二倍,251748, 包含着同样的数字,只是顺序不同. 找出最小的正整数x,使得 2x, 3x, 4x, 5x, 和6x都包含同样的数字. 这个题目相对比较简单 暴 ...

随机推荐

  1. 数据持久化-Plist文件写入

    数据持久化,常见4种:归档,plist文件,sqlite,coreData.今天复习的是plist文件读写. // // ViewController.m // Test_Plist // // Cr ...

  2. 二、secureCRT的 使用过程

    准备工作: win7与linux能互相ping通 linux安装了ssh被登陆服务 关闭window 防火墙,,控制面板 下载secureCRT 参考资料:http://zhidao.baidu.co ...

  3. [DHCP服务]——一个验证DHCP原理实验(VMware)

    大致实验拓扑图 DHCP Server端的配置 1. 安装DHCP # yum -y install dhcp 2. 拷贝配置文件 # /dhcpd.conf.sample /etc/dhcp/dhc ...

  4. c++ _beginthread

    c++多线程编程 #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #inclu ...

  5. 用C++编写一个随机产生多个两位数四则运算式子的简单程序

    一 设计思想: 1.首先可以想到一个四则运算式子的组成:两个运算数和一个运算符: 2.两个运算数的随机由调用随机函数产生,其中可以设定运算数的范围: 3.一个运算符的随机产生可以分为加减乘除四种情况, ...

  6. 随机的30道四则运算题(简单的c)

    #include <stdio.h>#include <stdlib.h>#include <time.h> int main(void){ int i = 0; ...

  7. windows phone和android,ios的touch事件兼容

    1.开发背景 最近用html5写了个小游戏,中间踩过无数坑,有很多甚至百度都百度不到答案,可见html5还真是不成熟,兼容性的复杂度比ie6有过之而无不及,性能那个渣简直无力吐槽.. 好了,吐槽结束, ...

  8. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  9. Codeforces Round #352 (Div. 2) C. Recycling Bottles 暴力+贪心

    题目链接: http://codeforces.com/contest/672/problem/C 题意: 公园里有两个人一个垃圾桶和n个瓶子,现在这两个人需要把所有的瓶子扔进垃圾桶,给出人,垃圾桶, ...

  10. matrix_last_acm_1

    password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=96950#problem/A 题意:n个数初始ai,m次操作 ...