C ++,622 553个字符
为了清楚起见,在下面添加了四个不必要的换行符。
#include"stdafx.h"
#include"string"
std::string c=" flush",d=" of a kind",e="straight",z[10]={"high card","one pair","two pair","three"+d,e,c,"full house","four"+d,e+c,"royal"+c},
x="CDHSA23456789TJQK";char h[99];int main(){__int64 f,p,t,g,u,v,w,l=1,a=78517370881,b=a+19173960,i,j,q=0;gets_s(h,99);for(i=28;i-->7;){f=p=0;
for(j=7;j--;)if(j!=i%7&j!=(i+i/7)%7){f+=l<<x.find(h[j*3+1])*6;p+=l<<x.find(h[j*3])*3-12;}
v=p&b*2;u=v&v-1;w=p&p/2;g=p*64&p*8&p&p/8&p/64;f&=f*4;t=f&&p==a?9:f&&g?8:p&b*4?7:u&&w?6:f?5:g||p==a?4:w?3:u?2:v?1:0;
q=t>q?t:q;}puts(z[q].c_str());}
高尔夫版本发生了一些变化:
修订版1:将所有数值变量更改__int64
为单个声明。
Rev 1:打高尔夫球的增量和for
循环条件
修订版0:将八进制常量更改为十进制。
修订版0:if
使用条件运算符将语句更改为赋值。修订版1:进一步将其重新排列为一个表达式t
。这需要v
中间值之一的新变量
修订版0:删除了详细输出。仅输出最佳的总手牌。
修订版0:放弃压缩输出文本(在C语言中比较困难,因为您无法使用+运算符来连接字符串。)只写一次“ flush”就为我节省了12个字符,却花了我15个字符,使我的整体效率降低了3个字符。所以我只写了3次。版本1:std::string
代替char[]
FDinoff的建议使用,可以与串联+
。
Ungolfed版本,714个非注释非空白字符。
循环使用21张可能由7张牌组成的牌,每次拒绝2张牌。五张所选牌的花色和等级在变量f和p中总计,每个花色/等级的八进制数字不同。执行各种位操作以确定指针的类型,然后将其存储在t中(所有21种可能性都以非高尔夫形式输出)。最后,输出可能的最佳指针。
#include "stdafx.h"
#include "string.h"
char x[] = "CDHSA23456789TJQK", h[99], z[10][99] =
{ "high card", "one pair", "two pair","three of a kind", "straight","flush","full house","four of a kind","straight","royal" };
int main(void)
{
int i,j,q=0; //i,j:loop counters. q:best possible hand of 7 card
scanf_s("%s/n", &h, 99); getchar();
for (i = 7; i < 28; i++){
//f,p: count number of cards of each suit (2 octal digits) and rank (1 octal digit.)
//t: best hand for current 5 cards. g:straight flag. u,w: flags for pairs and 3's.
//l: constant 1 (64bit leftshift doesn't work on a literal.)
//octal bitmasks: a=ace high straight, b=general purpose
__int64 f=0,p=0,t=0,g,u,w,l=1,a=01111000000001,b=a+0111111110;
for (j = 0; j < 7; j++){
if (j != i %7 & j != (i+i/7) %7){
f += l << (strchr(x,h[j*3+1])-x)*6;
p += l << (strchr(x,h[j*3])-x-4)*3;
printf_s("%c%c ",h[j*3], h[j*3+1]);
}
}
w=p&b*2; //if 2nd bit set we have a pair
if (w) t=1;
u= w & w-1; //if there is only one pair w&w-1 evaluates to 0; +ve for 2 pair.
if (u) t=2;
w = p & p/2; // if 2nd and 1st bit set we have 3 of kind.
if (w) t=3;
g = p*64 & p*8 & p & p/8 & p/64; // detects all straights except ace high. pattern for ace high in a.
if (g||p==a) t=4;
f&=f*4; //for a flush we want 5 cards of the same suit, binary 101
if (f) t=5;
if (u&&w) t=6; //full house meets conditions of 2 pair and 3 of kind
if (p & b*4) t=7; //four of a kind
if (f && g) t=8; //straight flush
if (f && p==a) t=9; //royal flush
printf_s("%s %s \n",z[t],t>7?z[5]:"");
q=t>q?t:q;
}
printf_s("%s %s",z[q],q>7?z[5]:"");
getchar();
}
非高尔夫输出