用STL中的next_permutation枚举出所有情况判断一下就OK
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct card
{
string str;
int v;
char ch;
}data[5];
int n;
void init()
{
int i;
for ( i = 0 ; i < 5 ; i++ )
{
cin>>data[i].str;
if ('2' <= data[i].str[0] && '9' >= data[i].str[0])
{
data[i].v= data[i].str[0]-'0';
data[i].ch=data[i].str[1];
}
else if ('1' == data[i].str[0])
{
data[i].v=10;
data[i].ch=data[i].str[2];
}
else if ( 'A' == data[i].str[0] )
{
data[i].v=1;
data[i].ch=data[i].str[1];
}
else if ( 'J' == data[i].str[0] )
{
data[i].v=11;
data[i].ch=data[i].str[1];
}
else if ('Q' == data[i].str[0])
{
data[i].v=12;
data[i].ch=data[i].str[1];
}
else if ('K' == data[i].str[0])
{
data[i].v=13;
data[i].ch=data[i].str[1];
}
}
}
bool cmp(card i, card j)
{
if (i.v != j.v)
return i.v < j.v;
else
return i.ch < j.ch;
}
int find()
{
if (data[0].ch != data[1].ch)
return 0;
int i, min=data[2].v, k=2, t=data[1].v;
for ( i = 3; i < 5; i++ )
{
if ( min > data[i].v )
{
min = data[i].v;
k= i;
}
}
if ( k == 2)
{
if ( data[3].v == data[4].v )
{
if ( data[3].ch > data[4].ch ) t+=3;
}
else
{
if (data[3].v > data[4].v) t+=3;
}
}
if ( k == 3)
{
if ( data[2].v == data[4].v )
{
if ( data[2].ch > data[4].ch ) t+=3;
}
else
{
if (data[2].v > data[4].v) t+=3;
}
}
if ( k == 4)
{
if ( data[2].v == data[3].v )
{
if ( data[2].ch > data[3].ch ) t+=3;
}
else
{
if (data[2].v > data[3].v) t+=3;
}
}
t+=k-1;
if ( t > 13) t-=13;
if ( t == data[0].v ) return 1;
else return 0;
}
void work()
{
sort(data, data+5, cmp);
do
{
if(find()) return;
}
while ( next_permutation(data, data+5, cmp));
}
int main()
{
scanf("%d", &n);
{
int i, k;
for ( k = 1 ; k <= n ; k++ )
{
init();
work();
printf("Problem %d:", k);
for ( i = 0 ; i < 5 ; i++ )
printf(" %s", data[i].str.c_str());
putchar(10);
}
}
return 0;
}