LightOj 1020 博弈

來源:互聯網
上載者:User

標籤:style   color   os   io   for   ar   2014   art   

思路介紹:

       1. 如果首先由Alice取,定義ans[i],如果ans[i]=1表示Alice會取勝,反之Bob取勝。枚舉前100項,ans[1]=0,ans[2]=1,ans[i]=!(ans[i-1]&&ans[i-2]);

          可以發現規律:當i為2,3,5,6,8,9....時Alice取勝,所以Alice取勝的條件為:i%3!=1;

      2.如果Bob先取,ans[1]=1,ans[2]=1,ans[i]=!(ans[i-1]&&ans[i-2])

        規律:當i為1,2,4,5,7,8...時Bob取勝,、;所以Bob取勝的條件為:i%3!=0;

 

1020 - A Childhood Game
PDF (English) Statistics Forum
Time Limit: 0.5 second(s) Memory Limit: 32 MB
Alice and Bob are playing a game with marbles; you may have played this game in childhood. The game is playing by alternating turns. In each turn a player can take exactly one or two marbles.

Both Alice and Bob know the number of marbles initially. Now the game can be started by any one. But the winning condition depends on the player who starts it. If Alice starts first, then the player who takes the last marble looses the game. If Bob starts first, then the player who takes the last marble wins the game.

Now you are given the initial number of marbles and the name of the player who starts first. Then you have to find the winner of the game if both of them play optimally.

Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer n (1 ≤ n < 231) and the name of the player who starts first.

Output
For each case, print the case number and the name of the winning player.

Sample Input
Output for Sample Input
3
1 Alice
2 Alice
3 Bob
Case 1: Bob
Case 2: Alice
Case 3: Alice


PROBLEM SETTER: JANE ALAM JAN

<span style="color:#6600cc;">/**************************************     author   : Grant Yuan     time     : 2014/8/20 13:38     algorithm: 博弈     source   : LightOj 1020***************************************/#include<bits/stdc++.h>#define Alice "Alice"#define Bob "Bob"using namespace std;char s[6];int n,t;int main(){    scanf("%d",&t);    for(int i=1;i<=t;i++)    {        memset(s,0,sizeof(s));        scanf("%d%s",&n,s);        printf("Case %d: ",i);        if(strcmp(s,Alice)==0){            if(n%3==1){                printf("Bob\n");            }            else{                printf("Alice\n");            }        }        else{            if(n%3==0){                printf("Alice\n");            }            else{                printf("Bob\n");            }        }    }    /*ans[1]=false;ans[2]=true;    for(int i=3;i<=100;i++)    {        ans[i]=true;        if(ans[i-1]&&ans[i-2])            ans[i]=false;    }    for(int i=1;i<=100;i++)    {        printf("%d ",i);        if(ans[i])            printf("1\n");        else            printf("0\n");    }*/    return 0;}</span>


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.