After losing all his chips when playing Texas Hold'em with Fsygd on the way to ZJOI2015, ZCC has just learned a black technology. Now ZCC is able to change all cards as he wants during the game. ZCC wants to get a Straight Flush by changing as few cards as possible.
Input
First line contains a single integer
T(T=1000)
which denotes the number of test cases.
For each test case, there are five short strings which denote the cards in a single line. It's guaranteed that all five cards are different.
Output
For each test case, output a single line which is the answer.
Sample Input
Sample Output
没看清题目,1可以当做是最大的,如:10,11,12,13,1这种情况可以当做是顺子(字母要一样)但11,12,13,1,2这样就不是顺子了,就这卡住了
思路:找两遍,花色就不用说了,只要遇到了不同花色就让ans++,第一遍找把当前牌的数字当成五张牌的最小值找一遍,如果两张牌的差值大于4则ans++,遍历五张牌取最小的ans;第二遍也是一样的,只不过是把当前牌当做是五张牌的最大值,不过这里要注意一点了,那就是10,11,12,13,1,,因为题目说了1可以当做是所有牌中的最大一个,这里我用的是特判。具体实现看代码。
下面再来看看这位大佬的代码,觉得比较好吧,大体思考方向都差不多
We call a five-card hand a Straight Flush when all five cards are consecutive and of the same suit. You are given a five-card hand. Please tell ZCC how many cards must be changed so as to get a Straight Flush.
Cards are represented by a letter('A', 'B', 'C', 'D') which denotes the suit and a number('1', '2',
⋯
, '13') which denotes the rank.
Note that number '1' represents ace which is the largest actually. "1 2 3 4 5" and "10 11 12 13 1" are both considered to be consecutive while "11 12 13 1 2" is not.
Note that number '1' represents ace which is the largest actually. "1 2 3 4 5" and "10 11 12 13 1" are both considered to be consecutive while "11 12 13 1 2" is not.
For each test case, there are five short strings which denote the cards in a single line. It's guaranteed that all five cards are different.
3 A1 A2 A3 A4 A5 A1 A2 A3 A4 C5 A9 A10 C11 C12 C13
0 1 2
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1e5+5;
typedef long long LL;
int vis[30];
struct node
{
char a;
int b;
}s[30];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int ans=10,ans1;
for(int i=0;i<5;i++)
{
getchar();
scanf("%c%d",&s[i].a,&s[i].b);
}
for(int i=0;i<5;i++)//第一遍找,把s[i].b当成最小值
{
ans1=0;
node tmp=s[i];
memset(vis,0,sizeof(vis));
vis[tmp.b]=1;
for(int j=0;j<5;j++)
{
if(j==i)
continue;
if(tmp.a!=s[j].a)
ans1++;
else
{
if(vis[s[j].b])//如果数字出现相同的也要换牌
{
ans1++;
continue;
}
vis[s[j].b]=1;
if(s[j].b-tmp.b>4||s[j].b-tmp.b<1)
ans1++;
}
}
ans=min(ans,ans1);
}
for(int i=0;i<5;i++)//第二遍找
{
ans1=0;
node tmp=s[i];
for(int j=0;j<5;j++)
memset(vis,0,sizeof(vis));
vis[tmp.b]=1;
for(int j=0;j<5;j++)
{
if(j==i)
continue;
if(tmp.a!=s[j].a)
ans1++;
else
{
if(vis[s[j].b])
{
ans1++;
continue;
}
vis[s[j].b]=1;
if(tmp.b==1)//1为最大值时的特判
{
if(!((tmp.b-s[j].b<=4&&tmp.b-s[j].b>=1)||(tmp.b-s[j].b>=-12&&tmp.b-s[j].b<=-9)))
ans1++;
}
else
{
if(!(tmp.b-s[j].b<=4&&tmp.b-s[j].b>=1))
ans1++;
}
}
}
ans=min(ans,ans1);
}
printf("%d\n",ans);
}
}
//附上样例
//99
//C1 A2 A3 A3 A5
//2
//A1 A5 A10 A11 A13
//1
//A1 A3 A6 A9 A11
//3
//A1 B3 B6 C9 D11
//3
//A1 A3 B6 C9 D11
//3
//A1 A6 B6 C9 D11
//4
//A1 A1 A1 A1 A1
//4
//A1 A2 A6 A9 A11
//3
下面再来看看这位大佬的代码,觉得比较好吧,大体思考方向都差不多
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
int MAX,cnt;
void CNT(int a[])//对于数组从第一个元素开始每五个扫一下 看五个需要换多少张 扫到结束 如a[1-5] a[2-6] 比较更新MAX
{
for(int i=1; i<=10; i++)
{
cnt=0;
for(int j=i; j<i+5; j++)
{
if(a[j]==0) cnt++;
}
if(cnt<MAX) MAX=cnt;
}
}
int main()
{
int a,T;
char c;
scanf("%d",&T);
while(T--)
{
int A[30]= {0},B[30]= {0},C[30]= {0},D[30]= {0};
int aa=0,bb=0,cc=0,dd=0;
for(int i=0; i<5; i++)
{
cin>>c>>a;
getchar();
if(c=='A')
{
A[a]=A[a+13]=1;
aa=1;
}
else if(c=='B')
{
B[a]=B[a+13]=1;
bb=1;
}
else if(c=='C')
{
C[a]=C[a+13]=1;
cc=1;
}
else
{
D[a]=D[a+13]=1;
dd=1;
}
}
MAX=4;
if(aa==1) CNT(A);
if(bb==1) CNT(B);
if(cc==1) CNT(C);
if(dd==1) CNT(D);
printf("%d\n",MAX);
}
return 0;
}