ABC 045 Card Game for Three

Problem Statement

Alice, Bob and Charlie are playing Card Game for Three, as below:

  • At first, each of the three players has a deck consisting of some number of cards. Each card has a letter a, b or c written on it. The orders of the cards in the decks cannot be rearranged.
  • The players take turns. Alice goes first.
  • If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says a, Alice takes the next turn.)
  • If the current player's deck is empty, the game ends and the current player wins the game.

You are given the initial decks of the players. More specifically, you are given three strings SA

, SB and SC. The i-th (1i|SA|) letter in SA is the letter on the i-th card in Alice's initial deck. SB and SC

describes Bob's and Charlie's initial decks in the same way.

Determine the winner of the game.

这是一道简单的模拟题,照着题目的意思做就可以。

但是,这题有一个需要注意的地方:<cstring>是C标准库头文件<string.h>的C++标准库版本声明的名称都是位于std中的,而<string>是C++的标准库中的string类型的头文件,只包含后者的时候如果使用了字符串相关的库函数(如strlen)会出问题(我在Vs上运行正常但提交就CE)。

以下为代码:

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <string>
 6 #include <cstring>
 7 #include <vector>
 8 using namespace std;
 9 char Sa[101], Sb[101], Sc[101];
10 int main()
11 {
12     scanf("%s", Sa);
13     scanf("%s", Sb);
14     scanf("%s", Sc);
15     int len1 = strlen(Sa), len2 = strlen(Sb), len3 = strlen(Sc);
16     int i = 0, j = 0, k = 0;//标记已弃掉的牌数-1
17     char c=Sa[i];
18     i++;
19     while (1)
20     {
21         if (c == 'a')
22         {
23             if (i == len1)//Alice已抽完
24             {
25                 printf("A");
26                 return 0;
27             }
28             c = Sa[i++];
29         }
30         if (c == 'b')//Bob已抽完
31         {
32             if (j == len2)
33             {
34                 printf("B");
35                 return 0;
36             }
37             c = Sb[j++];
38         }
39         if (c == 'c')//Charlie已抽完
40         {
41             if (k == len3)
42             {
43                 printf("C");
44                 return 0;
45             }
46             c = Sc[k++];
47         }
48     }
49     return 0;
50 }

 

posted @ 2021-04-02 22:49  许崇智  阅读(235)  评论(0)    收藏  举报