The Seventh Hunan Collegiate Programming Contest box game, hunancollegiate

Source: Internet
Author: User

The Seventh Hunan Collegiate Programming Contest box game, hunancollegiate

  • [1107] box games
  • Time Limit: 1000 MS memory limit: 65535 K
  • Problem description
  • There are two identical boxes, one with n balls and the other with one ball. Alice and Bob invented a game. The rule is as follows: Alice and Bob take turns to operate. When Alice first operates each operation, the gamer first checks which box contains fewer balls, then empty the box (the ball in the box is directly thrown away), and then take some balls from the other box into the box so that both boxes have at least one ball. If a player cannot perform operations, it will lose. Is a typical game:
    Bob cannot continue to operate on two boxes with one ball each, so Alice wins. Your task is to find out who will win. Assuming that both of them are smart, they always adopt the optimal strategy.
  • Input
  • The input can contain up to 300 groups of test data. Each group contains only one row of data, which contains an integer n (2 <=n <= 10 ^ 9 ). The input end flag is n = 0.
  • Output
  • For each group of data, the name of the winner is output.
  • Sample Input
  • 2 3 4 0
  • Sample output
  • Alice Bob Alice
  • Prompt
  • None
  • Source
  • The Seventh Hunan Collegiate Programming Contest
  • At the beginning of this question, I thought about how to calculate 1.1. Finally, I decided whether it was an odd or even number based on the number of required steps. If the odd number is alice's win, the even number is bob's win. Later I found that this idea was not feasible, later I changed my mind. Because an implicit condition of the question was not given, I mistakenly thought that as long as the number of two boxes is equal, I could not be divided, in fact, this idea is also wrong. It won't be able to be split until the two boxes are both 1, and we can deduce that when n = 2Bob wins at n-1;

  • Then we can quickly write the code:

  • #include <cstdio>#include <cstring>using namespace std;int main(){    int n;    while(scanf("%d",&n)&&n)    {        n++;        while(n%2==0)        {            n=n/2;        }        if(n==1)            printf("Bob\n");        else            printf("Alice\n");    }    return 0;}
    While (n & 1) = 0) // determine whether the Division can be 2 n> = 1; // divide 2






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.