題意:
(from luckycat)
解法:
直接模擬。
TAG: 模擬
注意:
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tittle: 10315 - Poker Hands | |
* Author: Cheng-Shih, Wong | |
* Date: 2015/06/17 | |
*/ | |
// include files | |
#include <bits/stdc++.h> | |
using namespace std; | |
// definitions | |
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i ) | |
#define clr(x,v) memset( x, v, sizeof(x) ) | |
class Card { | |
public: | |
char suit; | |
int val; | |
Card( char _v='1', char _s='1' ) { | |
val = toVal(_v); | |
suit = _s; | |
} | |
bool get( void ) { | |
char ts[10]; | |
if( scanf( "%s", ts )!=1 ) return false; | |
val = toVal(ts[0]); | |
suit = ts[1]; | |
return true; | |
} | |
int toVal( char _val ) { | |
switch( _val ) { | |
case '2': return 1; | |
case '3': return 2; | |
case '4': return 3; | |
case '5': return 4; | |
case '6': return 5; | |
case '7': return 6; | |
case '8': return 7; | |
case '9': return 8; | |
case 'T': return 9; | |
case 'J': return 10; | |
case 'Q': return 11; | |
case 'K': return 12; | |
case 'A': return 13; | |
} | |
} | |
const bool operator<( const Card &op ) const { | |
return val<op.val; | |
} | |
}; | |
typedef map<char,int> MCI; | |
typedef map<int,int> MII; | |
typedef pair<char,int> PCI; | |
typedef pair<int,int> PII; | |
// declarations | |
Card cards[15]; | |
// functions | |
PII cardValue( Card *card ) | |
{ | |
bool flush = true; | |
bool straight = true; | |
int four = 0; | |
int three = 0; | |
int pair = 0; | |
int pr[2]; | |
MCI suit; | |
MII val; | |
PII ret = PII(0,0); | |
sort( card, card+5 ); | |
FOR( i, 0, 3 ) | |
if( card[i].val+1 != card[i+1].val ) | |
straight = false; | |
FOR( i, 0, 4 ) { | |
suit[card[i].suit]++; | |
val[card[i].val]++; | |
} | |
if( suit.size()!=1 ) | |
flush = false; | |
for( PII p : val ) { | |
if( p.second==4 ) four = p.first; | |
else if( p.second==3 ) three = p.first; | |
else if( p.second==2 ) pr[pair++] = p.first; | |
} | |
if( pair==2 && pr[0]<pr[1] ) | |
swap( pr[0], pr[1] ); | |
int base = 1; | |
int allCards = 0; | |
FOR( i, 0, 4 ) { | |
allCards += base*card[i].val; | |
base *= 100; | |
} | |
if( straight && flush ) { | |
ret.first = 8; | |
ret.second = card[4].val; | |
} else if( four ) { | |
ret.first = 7; | |
ret.second = four; | |
} else if( three && pair ) { | |
ret.first = 6; | |
ret.second = three; | |
} else if( flush ) { | |
ret.first = 5; | |
ret.second = allCards; | |
} else if( straight ) { | |
ret.first = 4; | |
ret.second = card[4].val; | |
} else if( three ) { | |
ret.first = 3; | |
ret.second = three; | |
} else if( pair==2 ) { | |
ret.first = 2; | |
ret.second = pr[0]*10000+pr[1]*100; | |
for( PII p : val ) if( p.second==1 ) | |
ret.second += p.first; | |
} else if( pair==1 ) { | |
ret.first = 1; | |
ret.second = pr[0]*1000000; | |
base = 10000; | |
for( int i=4; i>=0; --i ) if( val[card[i].val]==1 ) { | |
ret.second += base*card[i].val; | |
base /= 100; | |
} | |
} else { | |
ret.first = 0; | |
ret.second = allCards; | |
} | |
return ret; | |
} | |
void solve( void ) | |
{ | |
PII white, black; | |
black = cardValue(cards+1); | |
white = cardValue(cards+6); | |
if( black > white ) puts("Black wins."); | |
else if( black < white ) puts("White wins."); | |
else puts("Tie."); | |
} | |
// main function | |
int main( void ) | |
{ | |
// input | |
while( cards[1].get() ) { | |
FOR( i, 2, 10 ) | |
cards[i].get(); | |
solve(); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言
任何意見都樂意傾聽