题目有意思,但是写起来挺复杂
先判断牌的类型,然后再比较
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
using namespace std;
///
#define INF 0xffffff7
#define maxn 1010
///
int card1Type, card2Type;
string card1[5];
string card2[5];
int cmp(string c1, string c2)
{
return c1[0] < c2[0];
}
int JudgeType(string cards[])
{
int i, j;
sort(cards, cards + 5, cmp);
//类型1 同花顺
for (i = 1; i < 5; i++)
{
if ((cards[i][0] - cards[i - 1][0] == 1) && (cards[i][1] == cards[i - 1][1]))
continue;
else
break;
}
if (i == 5)
return 1;
//类型2 四张
if (cards[0][0] == cards[1][0] && cards[1][0] == cards[2][0] && cards[2][0] == cards[3][0])
return 2;
if (cards[1][0] == cards[2][0] && cards[2][0] == cards[3][0] && cards[3][0] == cards[4][0])
return 2;
//类型3 葫芦
if (cards[0][0] == cards[1][0])
{
if (cards[2][0] == cards[3][0])
{
if (cards[4][0] == cards[3][0])
return 3;
}
}
if (cards[4][0] == cards[3][0])
{
if (cards[0][0] == cards[1][0])
{
if (cards[2][0] == cards[1][0])
return 3;
}
}
//类型4 同花
for (i = 0; i < 5; i++)
{
if (i != 0)
{
if (cards[i][1] == cards[i - 1][1])
continue;
else
break;
}
}
if (i == 5)
return 4;
//类型5 顺子
for (i = 1; i < 5; i++)
{
if (cards[i][0] - cards[i - 1][0] == 1)
continue;
else
break;
}
if (i == 5)
return 5;
//类型6 三张
if (cards[0][0] == cards[1][0] && cards[1][0] == cards[2][0])
return 6;
if (cards[1][0] == cards[2][0] && cards[2][0] == cards[3][0])
return 6;
if (cards[2][0] == cards[3][0] && cards[3][0] == cards[4][0])
return 6;
//类型7 两对
if (cards[0][0] == cards[1][0] && cards[2][0] == cards[3][0])
return 7;
if (cards[0][0] == cards[1][0] && cards[3][0] == cards[4][0])
return 7;
if (cards[1][0] == cards[2][0] && cards[3][0] == cards[4][0])
return 7;
//类型8 一对
int cnt = 0;
for (i = 1; i < 5; i++)
if (cards[i][0] == cards[i - 1][0])
cnt++;
if (cnt == 1)
return 8;
//9 一般牌
return 9;
}
int CompareCards(int t1, int t2)
{
int i, j;
if (t1 < t2)
return 1;
else if (t1 > t2)
return 2;
else
{
//1同花顺 只取决于最大牌的点数
if (t1 == 1)
{
if (card1[4][0] > card2[4][0])
return 1;
else if (card1[4][0] < card2[4][0])
return 2;
else
return 3;
}
//2四张 只取决于对子点数大小
if (t1 == 2)
{
if (card1[2][0] > card2[2][0])
return 1;
else if (card1[2][0] < card2[2][0])
return 2;
else
return 3;
}
//3葫芦 只取决于三张点数
if (t1 == 3)
{
if (card1[2][0] > card2[2][0])
return 1;
else if (card1[2][0] < card2[2][0])
return 2;
else
return 3;
}
//4同花
if (t1 == 4)
{
for (i = 4; i >= 0; i--)
{
if (card1[i][0] > card2[i][0])
return 1;
else if (card1[i][0] < card2[i][0])
return 2;
}
if (i < 0)
return 3;
}
//5顺子 只取决于最大牌的点数
if (t1 == 5)
{
if (card1[4][0] > card2[4][0])
return 1;
else if (card1[4][0] < card2[4][0])
return 2;
else
return 3;
}
//6三张 只取决于三张点数
if (t1 == 6)
{
if (card1[2][0] > card2[2][0])
return 1;
else if (card1[2][0] < card2[2][0])
return 2;
else
return 3;
}
//7两对
if (t1 == 7)
{
char c1pair1, c1pair2, c1num, c2pair1, c2pair2, c2num;
if (card1[0][0] == card1[1][0] && card1[2][0] == card1[3][0])
{
c1pair1 = card1[0][0];
c1pair2 = card1[2][0];
c1num = card1[4][0];
}
if (card1[0][0] == card1[1][0] && card1[3][0] == card1[4][0])
{
c1pair1 = card1[0][0];
c1pair2 = card1[3][0];
c1num = card1[2][0];
}
if (card1[1][0] == card1[2][0] && card1[3][0] == card1[4][0])
{
c1pair1 = card1[1][0];
c1pair2 = card1[3][0];
c1num = card1[0][0];
}
//
if (card2[0][0] == card2[1][0] && card2[2][0] == card2[3][0])
{
c2pair1 = card2[0][0];
c2pair2 = card2[2][0];
c2num = card2[4][0];
}
if (card2[0][0] == card2[1][0] && card2[3][0] == card2[4][0])
{
c2pair1 = card2[0][0];
c2pair2 = card2[3][0];
c2num = card2[2][0];
}
if (card2[1][0] == card2[2][0] && card2[3][0] == card2[4][0])
{
c2pair1 = card2[1][0];
c2pair2 = card2[3][0];
c2num = card2[0][0];
}
if (c1pair2 > c2pair2)
{
return 1;
}
else if (c1pair2 < c2pair2)
return 2;
else
{
if (c1pair1 > c2pair1)
return 1;
else if (c1pair1 < c2pair1)
return 2;
else
{
if (c1num > c2num)
return 1;
else if (c1num < c2num)
return 2;
else
return 3;
}
}
}
//8 一对
if (t1 == 8)
{
char c1Pair, c2Pair;
int c1pos, c2pos;
for (i = 1; i < 5; i++)
{
if (card1[i][0] == card1[i - 1][0])
{
c1Pair = card1[i][0];
c1pos = i;
}
if (card2[i][0] == card2[i - 1][0])
{
c2Pair = card2[i][0];
c2pos = i;
}
}
if (c1Pair > c2Pair)
return 1;
else if (c1Pair < c2Pair)
return 2;
else
{
for (i = 4; i >= 0; i--)
{
if ((i == c1pos) || i == (c1pos - 1))
continue;
if (card1[i][0] > card2[i][0])
return 1;
else if (card1[i][0] < card2[i][0])
return 2;
}
if (i < 0)
return 3;
}
}
//9 一般牌
for (i = 4; i >= 0; i--)
{
if (card1[i][0] > card2[i][0])
return 1;
else if (card1[i][0] < card2[i][0])
return 2;
}
if (i < 0)
return 3;
}
}
int main()
{
///
int i, j;
string line;
while (getline(cin, line))
{
istringstream stream(line);
for (i = 0; i < 5; i++)
{
stream >> card1[i];
switch (card1[i][0])
{
case 'T':
card1[i][0] = ':';
break;
case 'J':
card1[i][0] = ';';
break;
case 'Q':
card1[i][0] = '<';
break;
case 'K':
card1[i][0] = '=';
break;
case 'A':
card1[i][0] = '>';
break;
default:
break;
}
}
for (i = 0; i < 5; i++)
{
stream >> card2[i];
switch (card2[i][0])
{
case 'T':
card2[i][0] = ':';
break;
case 'J':
card2[i][0] = ';';
break;
case 'Q':
card2[i][0] = '<';
break;
case 'K':
card2[i][0] = '=';
break;
case 'A':
card2[i][0] = '>';
break;
default:
break;
}
}
card1Type = JudgeType(card1);
card2Type = JudgeType(card2);
int result = CompareCards(card1Type, card2Type);
if (result == 1)
cout << "Black wins." << endl;
else if (result == 2)
cout << "White wins." << endl;
else
cout << "Tie." << endl;
}
///
return 0;
}