猜数(简单的博弈)

题目链接:https://www.nowcoder.com/acm/contest/89/D

来源:牛客网

题目描述

There is a game consisted of two players Alice and Bob, and one referee Jelly.

At first, Jelly will choose a special integer X and write down on the paper, but don’t show it to each player. Then Jelly will give players two integers L and R, which means X ranges from L to R inclusively. Alice and Bob start to guess the integer X in turns, and Alice goes first. The first one who guesses the integer X loses the game.

The specific rules of the game are as follows:
One player guesses an integer K (K ∈ [L, R]), and Jelly will announce one of three results:
1. “Lose”. That means K is equal to X, this player loses the game.
2. “Large”. That means K is larger than X , other one should guess the integer from L to K − 1 inclusively in the next. (R becomes K − 1)
3. “Small.” That means K is smaller than X, other one should guess the integer from K+1 to R inclusively in the next. (L becomes K + 1)

If one player loses the game, Jelly will show players the paper with X so as to prevent Jelly to cheat the players.

One day, Alice and Bob play this game again. But they reach a consensus that the loser needs to pay for the winner’s shopping cart. Cause neither of them wants to lose the game, they order Braised Chicken and Rice(HuangMenJi) for Jelly at different times. In return, Jelly tells Alice and Bob the special integer X secretly. So, both players will use the best strategy to play the game.

Now, the question is who will win the game under this situation. (Alice goes first)

输入描述:

 
 
The first line contains an integer T, where T is the number of test cases. T test cases follow. 
For each test case, the only line contains three integers L, R and X.
• 1 ≤ T ≤ 10 5.

• 1≤L≤X≤R≤105.

输出描述:

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting

from 1) and y is the winner’s name.

题目大意:两个人猜数,这个数字是已知的,在一个给定的范围内,l-r谁先说出这个数字谁就输了;

分析一波题目,很明显,先说出key+1或key-1的那个人就输了(有这样的数字存在)

然后可以推出:先说出key+2或key-2的那个人就输了。。。。

然后发现只要以key为中心,有对称的数存在,那么先说出这样的数的人就输了,因此,如果key正好在中间,那么先说数的人就输了,因为下一个人总能将他逼到key这个数字上;

反之,如果不是对称的,那么先说的人就赢了,因为先说的人可以直接将他逼到对称的数字上

ac:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
 
//#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
 
#define ll long long
#define da    0x3f3f3f3f
#define xiao -0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))// 雷打不动的头文件
 
 
 
int main()
{
    int t,k=1;;
    cin>>t;
    while(t--)
    {
        int l,r,x;
        cin>>l>>r>>x;
        if(l==r)
            cout<<"Case #"<<k++<<": Bob"<<endl;
        else if(x==r||x==l)        //显而易见的答案
            cout<<"Case #"<<k++<<": Alice"<<endl;
        else
        {
            int lenr=r-x;
            int lenl=x-l;
            if(lenr==lenl)        //对称分布
                cout<<"Case #"<<k++<<": Bob"<<endl;
            else                    //不是对称的数字
                cout<<"Case #"<<k++<<": Alice"<<endl;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值