game theory

Bash game:

Tang and Jiang are good friends. To decide whose treat it is for dinner, they are playing a game. Specifically, Tang and Jiang will alternatively write numbers (integers) on a white board. Tang writes first, then Jiang, then again Tang, etc... Moreover, assuming that the number written in the previous round is X, the next person who plays should write a number Y such that 1 <= Y - X <= k. The person who writes a number no smaller than N first will lose the game. Note that in the first round, Tang can write a number only within range [1, k] (both inclusive). You can assume that Tang and Jiang will always be playing optimally, as they are both very smart students.

 

InputThere are multiple test cases. For each test case, there will be one line of input having two integers N (0 < N <= 10^8) and k (0 < k <= 100). Input terminates when both N and k are zero.
OutputFor each case, print the winner's name in a single line.
Sample Input

1 1
30 3
10 2
0 0

Sample Output

Jiang
Tang
Jiang

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#define ll long long int
using namespace std;

intmain ()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0&&m==0)
            break;
        if( !( (n-1)%(m+1) ) )
            printf("Jiang\n");
        else
            printf("Tang\n");
    }
    return 0;
}

Taken from: https://blog.csdn.net/u011613321/article/details/12142861

 

 

Wyzhov game:

There are two piles of stones, the number of which can be different. The game begins with two people taking turns taking stones. The game stipulates that there are two different ways to take each time. One is to take as many stones as you want from any pile; the other is to take the same number of stones from two piles at the same time. In the end, the one who removes all the stones is the winner. Now given the initial number of two piles of stones, if it is your turn to take first, assuming both sides take the best strategy, ask whether you are the winner or the loser in the end.

The Input input contains several lines, representing the initial conditions of several kinds of stones, each of which contains two non-negative integers a and b, representing the number of two piles of stones, a and b are not greater than 1,000,000,000. The Output output also has several lines, each line contains a number 1 or 0, if you are the winner in the end, it is 1, otherwise, it is 0. Sample Input

2 1
8 4
4 7

Sample Output

0
1
0

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
double p=(sqrt((double)5)+1)/double(2);
int main (){
    int a,b,c;
        while(scanf("%d%d",&a,&b)!=EOF){
        c=abs(a-b);
        a=a>b?b:a;
        if(a==(int)(p*c)) printf("0\n");
        else printf("1\n");
    }
    return 0;
}

 

Taken from: https://blog.csdn.net/dgq8211/article/details/7397876

 

 

Fibonacci game:

There are n stones in a pile, and two people take turns to take them. The first one can take any number of stones for the first time, but they cannot take all of them. The number of stones taken each time thereafter cannot exceed 2 times the number of stones taken last time. Whoever takes the finish wins. Whoever takes first loses and outputs "Second win". Whoever wins first outputs "First win".

Input has multiple groups. The first line of each group is 2<=n<2^31. n=0 exits.
Output The first to take the negative output "Second win". The first to win the output "First win".
See Sample Output.
Sample Input

2
13
10000
0
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <climits>
using namespace std;

int a,b;
int main()
{
    int n,ts;
    bool mark;
    while(scanf("%d",&n) && n){
        a = 2,b = 3;
        mark = false;
        while(a<=n){
            if(a==n || b==n){
                mark = true;
                break;
            }
            ts = (a  + b);
            a = b;
            b = ts;
        }
        if(mark){
            printf("Second win\n");
        }else{
            printf("First win\n");
        }
    }
    return 0;
}

To deepen your understanding, please click here>> https://www.cnblogs.com/jiu0821/p/4638165.html <<

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324612149&siteId=291194637