POJ 2200 A Card Trick(模拟)

时间:2023-03-09 22:39:08
POJ 2200 A Card Trick(模拟)

题目链接

题意 : 一共52张牌(A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K)花色分别是C,D,H,S 。。。给助理5张牌,然后助理需要重新排一下次序,把第一张牌给观众,然后让魔术师根据一个规律对剩下的有一定次序的牌,能够猜出观众手里的牌是哪张。规律是:

  1. 记下剩下的四张牌里第一张的值和花色。
  2. 然后剩下三张,找出最小的那张(按值大小排,如果值一样大按照花色排序,花色的顺序是CDHS)所在的位置然后把这个位置的值加到原来记下的第一张牌的值上。
  3. 除了最小的那张,还有两张大的,如果这两张是有序的就把将第二步得到的值再加3.
  4. 所以观众手里的牌的值就是经过上述三步加起来的值,花色就是原来记下的第一张牌的花色。

比如说 4D 5H 10C 10D QH,助理需要按照5H QH 10D 10C 4D这个顺序,将5H给观众,然后将QH 10D 10C 4D给魔术师,因为魔术师手里第一张牌的花色是H,所以观众手里那张牌的花色是H,然后魔术师手里的第一张值是12,加上剩下的牌里最小的4D所在的位置3,是15,取完余就是2,然后因为10D和10C是无序的,所以要再加3,就是5,所以观众手里的牌是5H .

思路 : 这个题要猜的话不怎么好猜,所以就是两个循环枚举一下,然后再处理一下小细节什么的。取余那个地方有特例所以要注意

 //POJ 2200
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm> using namespace std ; char str[][] ;
//char ch[5][4] ;
int e ;
struct node
{
int num ;
int flo ;
} a[],ch[] ; int cmp(struct node a,struct node b)
{
if(a.num == b.num)
return a.flo < b.flo ;
return a.num < b.num ;
}
void inv()
{
for(int j = ; j < ; j++)
{
int len = strlen(str[j]) ;
if(len == ) a[j].num = ;
if(str[j][] == 'J') a[j].num = ;
else if(str[j][] == 'Q') a[j].num = ;
else if(str[j][] == 'K') a[j].num = ;
else if(str[j][] == 'A') a[j].num = ;
else if(str[j][] >= '' && str[j][] <= '') a[j].num = str[j][]-'' ;
if(str[j][len-] == 'C') a[j].flo = ;
if(str[j][len-] == 'D') a[j].flo = ;
if(str[j][len-] == 'H') a[j].flo = ;
if(str[j][len-] == 'S') a[j].flo = ; }
} void judge(struct node a)
{
if(a.num == )
printf("%c",'A') ;
else if(a.num == )
printf("") ;
else if(a.num == )
printf("J") ;
else if(a.num == )
printf("Q") ;
else if(a.num == )
printf("K") ;
else printf("%d",a.num) ;
if(a.flo == )
printf("S") ;
else if(a.flo == )
printf("H") ;
else if(a.flo == )
printf("D") ;
else if(a.flo == )
printf("C") ;
}
void solve()
{
int j ;
for(int i = ; i < ; i++)
{
for(j = ; j < ; j++)
{
if(i != j && a[i].flo == a[j].flo)
{
e = ;
for(int k = ; k < ; k++)
{ if(k != i && k != j)
{
ch[e].num = a[k].num ;
ch[e].flo = a[k].flo ;
e++ ;
//strcpy(ch[e++],str[k]) ;
}
}
sort(ch,ch+e,cmp) ;
int a1 = a[i].num ;
int a2 = a[j].num ;
if( a1 == (a2+)% || (a1 == &&a2 == ) )
{
printf("%s %s ",str[i],str[j]);
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf("\n") ;
break;
}
else if(a1 == (a2+)% || (a1 == && a2 == ) )
{
printf("%s %s ",str[i],str[j]);
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf("\n") ;
break;
}
else if(a1 == (a2+)% || (a1 == && a2 == ) )
{
printf("%s %s ",str[i],str[j]);
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf("\n") ;
break;
}
else if(a1 == (a2+)% || (a1 == && a2 == ) )
{
printf("%s %s ",str[i],str[j]);
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf("\n") ;
break;
}
else if(a1 == (a2+)% || (a1 == && a2 == ) )
{
printf("%s %s ",str[i],str[j]);
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf("\n") ;
break;
}
else if(a1 == (a2+)% || (a1 == && a2 == ) )
{
printf("%s %s ",str[i],str[j]);
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf(" ") ;
judge(ch[]) ;
printf("\n") ;
break;
}
}
}
if(j < ) break ;
}
}
int main()
{
int n ;
scanf("%d",&n) ;
for(int i = ; i <= n ; i++ )
{
for(int j = ; j < ; j++)
scanf("%s",str[j]) ;
inv() ;
printf("Problem %d: ",i) ;
solve() ;
}
return ;
}

这个是比赛的时候一宁手敲出来的

 #include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std; struct node
{
int num,flo;
} input[],temp[];
int cmp(const void *a,const void *b)
{
struct node *aa=(struct node *)a;
struct node *bb=(struct node *)b;
if(aa->num==bb->num)return aa->flo-bb->flo;
else return aa->num-bb->num;
}
char s[];
int flocnt[];
int judge(int p)
{
if(s[p]=='A')
return ;
else if(s[p]=='J')
return ;
else if(s[p]=='Q')
return ;
else if(s[p]=='K')
return ;
else if(s[p]=='C')
return ;
else if(s[p]=='D')
return ;
else if(s[p]=='H')
return ;
else if(s[p]=='S')
return ;
else return s[p]-''-;
}
bool vis[],flat;
bool max11(node a,node b)
{
if(a.num==b.num)
{
if(a.flo<b.flo)return true;
else return false;
}
else if(a.num<b.num)return true;
else return false;
}
void dfs(int x)
{
if(flat)
return ;
if(x==)
{
for(int i=; i<; i++)
{
if(flocnt[input[i].flo]>&&!flat)
{
temp[]=input[i];
vis[i]=true;
dfs();
vis[i]=false;
}
}
return ;
}
else if(x==)
{
for(int i=; i<; i++)
{
if(!vis[i]&&input[i].flo==temp[].flo&&!flat)
{
temp[]=input[i];
vis[i]=true;
dfs();
vis[i]=false;
}
}
}
else if(x==)
{
struct node sorted[];
struct node xxx=temp[];
for(int i=; i<; i++)
sorted[i-]=temp[i];
qsort(sorted,,sizeof(sorted[]),cmp);
int pp;
for(int i=; i<; i++)
{
if(temp[i].flo==sorted[].flo&&temp[i].num==sorted[].num)
{
pp=i;
break;
}
}
if(pp==)
{
xxx.num+=;
if(max11(temp[],temp[]))
{ }
else
xxx.num+=;
xxx.num%=;
if(xxx.flo==temp[].flo&&xxx.num==temp[].num)flat=true;
return;
}
else if(pp==)
{
xxx.num+=;
if(max11(temp[],temp[]))
{ }
else
xxx.num+=;
xxx.num%=;
if(xxx.flo==temp[].flo&&xxx.num==temp[].num)flat=true;
return;
}
else if(pp==)
{
xxx.num+=;
if(max11(temp[],temp[]))
{ }
else
xxx.num+=;
xxx.num%=;
if(xxx.flo==temp[].flo&&xxx.num==temp[].num)flat=true;
return;
}
}
else
{
for(int i=; i<; i++)
{
if(!vis[i]&&!flat)
{
temp[x]=input[i];
vis[i]=true;
dfs(x+);
vis[i]=false;
}
}
} }
void pri(int x)
{
if(temp[x].num==)printf("A");
else if(temp[x].num==)printf("J");
else if(temp[x].num==)printf("Q");
else if(temp[x].num==)printf("K");
else printf("%d",temp[x].num+);
if(temp[x].flo==)printf("C");
else if(temp[x].flo==)printf("D");
else if(temp[x].flo==)printf("H");
else if(temp[x].flo==)printf("S");
}
int main()
{
//freopen("data.in","r",stdin);
int t,len;
scanf("%d",&t);
while(t--)
{
memset(flocnt,,sizeof(flocnt));
for(int i=; i<; i++)
{
scanf("%s",s);
len=strlen(s);
if(len==)
{
input[i].flo=judge();
input[i].num=;
}
else
{
input[i].flo=judge();
input[i].num=judge();
}
flocnt[input[i].flo]++;
}
memset(vis,false,sizeof(vis));
flat=false;
dfs();
for(int i=; i<; i++)
{
pri(i);
printf(" ");
}
pri();
puts("");
}
return ;
}