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 .

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

  1 //POJ 2200

  2 #include <stdio.h>

  3 #include <string.h>

  4 #include <iostream>

  5 #include <algorithm>

  6 

  7 using namespace std ;

  8 

  9 char str[5][4] ;

 10 //char ch[5][4] ;

 11 int e ;

 12 struct node

 13 {

 14     int num ;

 15     int flo ;

 16 } a[10],ch[10] ;

 17 

 18 int cmp(struct node a,struct node b)

 19 {

 20     if(a.num == b.num)

 21         return a.flo < b.flo ;

 22     return a.num < b.num ;

 23 }

 24 void inv()

 25 {

 26     for(int j = 0 ; j < 5 ; j++)

 27     {

 28         int len = strlen(str[j]) ;

 29         if(len == 3) a[j].num = 10;

 30         if(str[j][0] == 'J')  a[j].num = 11 ;

 31         else if(str[j][0] == 'Q')  a[j].num = 12 ;

 32         else if(str[j][0] == 'K')  a[j].num = 13 ;

 33         else if(str[j][0] == 'A') a[j].num = 1 ;

 34         else if(str[j][0] >= '2' && str[j][0] <= '9')  a[j].num = str[j][0]-'0' ;

 35         if(str[j][len-1] == 'C') a[j].flo = 1 ;

 36         if(str[j][len-1] == 'D')  a[j].flo = 2 ;

 37         if(str[j][len-1] == 'H') a[j].flo = 3 ;

 38         if(str[j][len-1] == 'S')  a[j].flo = 4 ;

 39 

 40     }

 41 }

 42 

 43 void judge(struct node a)

 44 {

 45     if(a.num == 1)

 46         printf("%c",'A') ;

 47     else if(a.num == 10)

 48         printf("10") ;

 49     else if(a.num == 11)

 50         printf("J") ;

 51     else if(a.num == 12)

 52         printf("Q") ;

 53     else if(a.num == 13)

 54         printf("K") ;

 55     else printf("%d",a.num) ;

 56     if(a.flo == 4)

 57         printf("S") ;

 58     else if(a.flo == 3)

 59         printf("H") ;

 60     else if(a.flo == 2)

 61         printf("D") ;

 62     else if(a.flo == 1)

 63         printf("C") ;

 64 }

 65 void solve()

 66 {

 67     int j ;

 68     for(int i = 0 ; i < 5 ; i++)

 69     {

 70         for(j = 0 ; j < 5 ; j++)

 71         {

 72             if(i != j && a[i].flo == a[j].flo)

 73             {

 74                 e = 0 ;

 75                 for(int k = 0 ; k < 5 ; k++)

 76                  {   if(k != i && k != j)

 77                     {

 78                         ch[e].num = a[k].num ;

 79                         ch[e].flo = a[k].flo ;

 80                         e++ ;

 81                         //strcpy(ch[e++],str[k]) ;

 82                     }

 83                  }

 84                 sort(ch,ch+e,cmp) ;

 85                 int a1 = a[i].num ;

 86                 int a2 = a[j].num ;

 87                 if( a1 == (a2+1)%13 || (a1 == 13&&a2 == 12) )

 88                 {

 89                     printf("%s %s ",str[i],str[j]);

 90                     judge(ch[0]) ;

 91                     printf(" ") ;

 92                     judge(ch[1]) ;

 93                     printf(" ") ;

 94                     judge(ch[2]) ;

 95                     printf("\n") ;

 96                     break;

 97                 }

 98                 else if(a1 == (a2+2)%13 || (a1 == 13 && a2 == 11) )

 99                 {

100                     printf("%s %s ",str[i],str[j]);

101                     judge(ch[1]) ;

102                     printf(" ") ;

103                     judge(ch[0]) ;

104                     printf(" ") ;

105                     judge(ch[2]) ;

106                     printf("\n") ;

107                     break;

108                 }

109                 else if(a1 == (a2+3)%13 || (a1 == 13 && a2 == 10) )

110                 {

111                     printf("%s %s ",str[i],str[j]);

112                     judge(ch[1]) ;

113                     printf(" ") ;

114                     judge(ch[2]) ;

115                     printf(" ") ;

116                     judge(ch[0]) ;

117                     printf("\n") ;

118                     break;

119                 }

120                 else if(a1 == (a2+4)%13 || (a1 == 13 && a2 == 9) )

121                 {

122                     printf("%s %s ",str[i],str[j]);

123                     judge(ch[0]) ;

124                     printf(" ") ;

125                     judge(ch[2]) ;

126                     printf(" ") ;

127                     judge(ch[1]) ;

128                     printf("\n") ;

129                     break;

130                 }

131                 else if(a1 == (a2+5)%13 || (a1 == 13 && a2 == 8) )

132                 {

133                     printf("%s %s ",str[i],str[j]);

134                     judge(ch[2]) ;

135                     printf(" ") ;

136                     judge(ch[0]) ;

137                     printf(" ") ;

138                     judge(ch[1]) ;

139                     printf("\n") ;

140                     break;

141                 }

142                 else if(a1 == (a2+6)%13 || (a1 == 13 && a2 == 7) )

143                 {

144                     printf("%s %s ",str[i],str[j]);

145                     judge(ch[2]) ;

146                     printf(" ") ;

147                     judge(ch[1]) ;

148                     printf(" ") ;

149                     judge(ch[0]) ;

150                     printf("\n") ;

151                     break;

152                 }

153             }

154         }

155         if(j < 5) break ;

156     }

157 }

158 int main()

159 {

160     int n ;

161     scanf("%d",&n) ;

162     for(int i = 1 ; i <= n ; i++ )

163     {

164         for(int j = 0 ; j < 5 ; j++)

165             scanf("%s",str[j]) ;

166         inv() ;

167         printf("Problem %d: ",i) ;

168         solve() ;

169     }

170     return 0 ;

171 }
View Code

 

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

  1 #include <iostream>

  2 #include <algorithm>

  3 #include <cstdlib>

  4 #include <cstdio>

  5 #include <cstring>

  6 #include <cmath>

  7 

  8 

  9 using namespace std;

 10 

 11 struct node

 12 {

 13     int num,flo;

 14 } input[5],temp[5];

 15 int cmp(const void *a,const void *b)

 16 {

 17     struct node *aa=(struct node *)a;

 18     struct node *bb=(struct node *)b;

 19     if(aa->num==bb->num)return aa->flo-bb->flo;

 20     else return aa->num-bb->num;

 21 }

 22 char s[5];

 23 int flocnt[5];

 24 int judge(int p)

 25 {

 26     if(s[p]=='A')

 27         return 0;

 28     else if(s[p]=='J')

 29         return 10;

 30     else if(s[p]=='Q')

 31         return 11;

 32     else if(s[p]=='K')

 33         return 12;

 34     else if(s[p]=='C')

 35         return 1;

 36     else if(s[p]=='D')

 37         return 2;

 38     else if(s[p]=='H')

 39         return 3;

 40     else if(s[p]=='S')

 41         return 4;

 42     else return s[p]-'0'-1;

 43 }

 44 bool vis[10],flat;

 45 bool max11(node a,node b)

 46 {

 47     if(a.num==b.num)

 48     {

 49         if(a.flo<b.flo)return true;

 50         else return false;

 51     }

 52     else if(a.num<b.num)return true;

 53     else return false;

 54 }

 55 void dfs(int x)

 56 {

 57     if(flat)

 58         return ;

 59     if(x==0)

 60     {

 61         for(int i=0; i<5; i++)

 62         {

 63             if(flocnt[input[i].flo]>1&&!flat)

 64             {

 65                 temp[0]=input[i];

 66                 vis[i]=true;

 67                 dfs(1);

 68                 vis[i]=false;

 69             }

 70         }

 71         return ;

 72     }

 73     else if(x==1)

 74     {

 75         for(int i=0; i<5; i++)

 76         {

 77             if(!vis[i]&&input[i].flo==temp[0].flo&&!flat)

 78             {

 79                 temp[1]=input[i];

 80                 vis[i]=true;

 81                 dfs(2);

 82                 vis[i]=false;

 83             }

 84         }

 85     }

 86     else if(x==5)

 87     {

 88         struct node sorted[3];

 89         struct node xxx=temp[1];

 90         for(int i=2; i<5; i++)

 91             sorted[i-2]=temp[i];

 92         qsort(sorted,3,sizeof(sorted[0]),cmp);

 93         int pp;

 94         for(int i=2; i<5; i++)

 95         {

 96             if(temp[i].flo==sorted[0].flo&&temp[i].num==sorted[0].num)

 97             {

 98                 pp=i;

 99                 break;

100             }

101         }

102         if(pp==2)

103         {

104             xxx.num+=1;

105             if(max11(temp[3],temp[4]))

106             {

107 

108             }

109             else

110                 xxx.num+=3;

111             xxx.num%=13;

112             if(xxx.flo==temp[0].flo&&xxx.num==temp[0].num)flat=true;

113             return;

114         }

115         else if(pp==3)

116         {

117             xxx.num+=2;

118             if(max11(temp[2],temp[4]))

119             {

120 

121             }

122             else

123                 xxx.num+=3;

124             xxx.num%=13;

125             if(xxx.flo==temp[0].flo&&xxx.num==temp[0].num)flat=true;

126             return;

127         }

128         else if(pp==4)

129         {

130             xxx.num+=3;

131             if(max11(temp[2],temp[3]))

132             {

133 

134             }

135             else

136                 xxx.num+=3;

137             xxx.num%=13;

138             if(xxx.flo==temp[0].flo&&xxx.num==temp[0].num)flat=true;

139             return;

140         }

141     }

142     else

143     {

144         for(int i=0; i<5; i++)

145         {

146             if(!vis[i]&&!flat)

147             {

148                 temp[x]=input[i];

149                 vis[i]=true;

150                 dfs(x+1);

151                 vis[i]=false;

152             }

153         }

154     }

155 

156 }

157 void pri(int x)

158 {

159     if(temp[x].num==0)printf("A");

160     else if(temp[x].num==10)printf("J");

161     else if(temp[x].num==11)printf("Q");

162     else if(temp[x].num==12)printf("K");

163     else printf("%d",temp[x].num+1);

164     if(temp[x].flo==1)printf("C");

165     else if(temp[x].flo==2)printf("D");

166     else if(temp[x].flo==3)printf("H");

167     else if(temp[x].flo==4)printf("S");

168 }

169 int main()

170 {

171     //freopen("data.in","r",stdin);

172     int t,len;

173     scanf("%d",&t);

174     while(t--)

175     {

176         memset(flocnt,0,sizeof(flocnt));

177         for(int i=0; i<5; i++)

178         {

179             scanf("%s",s);

180             len=strlen(s);

181             if(len==3)

182             {

183                 input[i].flo=judge(2);

184                 input[i].num=9;

185             }

186             else

187             {

188                 input[i].flo=judge(1);

189                 input[i].num=judge(0);

190             }

191             flocnt[input[i].flo]++;

192         }

193         memset(vis,false,sizeof(vis));

194         flat=false;

195         dfs(0);

196         for(int i=0; i<4; i++)

197         {

198             pri(i);

199             printf(" ");

200         }

201         pri(4);

202         puts("");

203     }

204     return 0;

205 }
View Code

 

 

你可能感兴趣的:(poj)