题目大意:手上有5张牌, 堆上有5张牌, 现在要求舍弃手上的n(0<=n<=5)牌, 从堆的面上拿n张牌, 假设了你有特异功能, 可以知道堆上的5张牌从上到下是是什么, 要求最后得到的牌要面值最大, 一张牌由数字与花色组成。
straight-flush 同花顺:一手同花的五张牌 + 五张牌点数连续的顺子
four-of-a-kind 四张相同 的牌
full-house 满堂红:三张同点牌加上一对(此时存在2张不同点的牌)
flush 一手同花的五张牌
straight 顺子:五张牌点数连续的顺子
three-of-a-kind 三张相同的牌
two-pairs 两对对子(此时存在3张不同点的牌)
one-pair 一对对子
highest-card 上面的所有情况不符合
( 坑爹的一道题 , 注意A的俩个身份 既是A又是 1 。。。。。 )
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <ctype.h> 5 #include <time.h> 6 #include <math.h> 7 char str[15][5]; 8 char sub[8][5]; 9 bool mark[10]; 10 int cur; 11 12 char ch[15][25] = { 13 "straight-flush", 14 "four-of-a-kind", 15 "full-house", 16 "flush", 17 "straight", 18 "three-of-a-kind", 19 "two-pairs", 20 "one-pair", 21 "highest-card" 22 }; 23 24 bool Flush() //同花 25 { 26 char s = sub[0][1]; 27 for( int i = 1; i < 5; ++i ) 28 if( s != sub[i][1] ) 29 return false; 30 return true; 31 } 32 33 bool Straight() //顺子 34 { 35 if( sub[0][0] == ‘2‘ ) 36 { 37 for( int i = 1; i < 4; ++i ) 38 if( sub[i][0] != ‘2‘ + i ) 39 return false; 40 if( sub[4][0] == ‘>‘ || sub[4][0] == ‘6‘ ) 41 return true; 42 return false; 43 } 44 for( int i = 0; i < 4; ++i ) 45 if( sub[i+1][0] != sub[i][0] + 1 ) 46 return false; 47 return true; 48 } 49 50 int Kind() //相同 51 { 52 int num[7]; 53 memset( num, 0, sizeof( num ) ); 54 int t = 0; 55 for( int i = 1; i < 5; ++i ) 56 if( sub[i][0] == sub[i-1][0] ) 57 ++num[t]; 58 else 59 ++t; 60 t = 0; 61 int Max = 0; 62 for( int i = 0; i <= 4; ++i ) 63 if( num[i] ) { 64 if( Max < num[i] ) 65 Max = num[i]; 66 ++t; 67 } 68 if( Max >= 3 ) 69 return 4; 70 if( Max == 2 && t == 2 ) 71 return 5; 72 if( Max == 2 ) 73 return 3; 74 if( Max == 1 && t == 2 ) 75 return 2; 76 if( Max == 1 ) 77 return 1; 78 return 0; 79 } 80 81 void Sort() 82 { 83 for( int i = 1; i < 5; ++i ) 84 for( int j = 0; j < 5-i; ++j ) 85 if( sub[j][0] > sub[j+1][0] ) 86 { 87 char ss[5]; 88 strcpy( ss, sub[j] ); 89 strcpy( sub[j], sub[j+1] ); 90 strcpy( sub[j+1], ss ); 91 } 92 } 93 94 int Search() 95 { 96 Sort(); 97 if( Flush() && Straight() ) 98 return 0; 99 int k = Kind(); 100 if( k == 4 ) 101 return 1; 102 if( k == 5 ) 103 return 2; 104 if( Flush() ) 105 return 3; 106 if( Straight() ) 107 return 4; 108 if( k == 3 ) 109 return 5; 110 if( k == 2 ) 111 return 6; 112 if( k == 1 ) 113 return 7; 114 return 8; 115 } 116 117 void dfs( int x ) 118 { 119 if( x == 5 ) { 120 int y = 5; 121 for( int i = 0; i < 5; ++i ) 122 if( mark[i] ) 123 strcpy( sub[i], str[i] ); 124 else 125 strcpy( sub[i], str[y++] ); 126 int k = Search(); 127 if( cur > k ) 128 cur = k; 129 return; 130 } 131 mark[x] = 1; 132 dfs( x+1 ); 133 mark[x] = 0; 134 dfs( x+1 ); 135 } 136 137 void Change( char x ) 138 { 139 if( str[x][0] == ‘T‘ ) 140 str[x][0] = ‘:‘; 141 else if( str[x][0] == ‘J‘ ) 142 str[x][0] = ‘;‘; 143 else if( str[x][0] == ‘Q‘ ) 144 str[x][0] = ‘<‘; 145 else if( str[x][0] == ‘K‘ ) 146 str[x][0] = ‘=‘; 147 else if( str[x][0] == ‘A‘ ) 148 str[x][0] = ‘>‘; 149 } 150 151 int main() 152 { 153 while( ~scanf( "%s", str[0] ) ) 154 { 155 memset( sub, ‘\0‘, sizeof( sub ) ); 156 memset( mark, 0, sizeof( mark ) ); 157 printf( "Hand: %s", str[0] ); 158 Change( 0 ); 159 for( int i = 1; i < 10; ++i ) 160 { 161 getchar(); 162 scanf( "%s", str[i] ); 163 if( i == 5 ) 164 printf( " Deck:" ); 165 printf( " %s", str[i] ); 166 Change( i ); 167 } 168 cur = 8; 169 getchar(); 170 dfs( 0 ); 171 printf( " Best hand: %s\n", ch[cur] ); 172 } 173 return 0; 174 }
时间: 2024-10-19 09:04:54