第十四届浙江财经大学程序设计竞赛 D Disport with Jelly【NIM】

本文介绍了一个基于博弈论的游戏,玩家需猜出裁判选定的整数。通过制定最优策略,文章探讨了不同情况下胜者的变化,并提供了一段C++代码实现。游戏规则包括玩家轮流猜测并依据裁判反馈调整范围。

链接:https://www.nowcoder.com/acm/contest/89/D
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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 ≤ 105.
• 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.
示例1
输入
2
1 1 1
1 2 2
输出
Case #1: Bob
Case #2: Alice
备注:
For the first case, Alice can only guess the integer 1, so she loses the game, the winner is Bob.
For the second case, if Alice guesses the integer 1, then Bob can only guess the integer 2. So the winner
is Alice.

题意:NIM博弈

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);int cs=0;
    while(T--){
            cs++;
        int L,R,X;
        scanf("%d%d%d",&L,&R,&X);
        L=X-L;
        R=R-X;
        printf("Case #%d: ",cs);
        if(L==R)
            printf("Bob\n");
        else printf("Alice\n");
    }
    return 0;
}
#include <reg51.h> #include <stdlib.h> #define uchar unsigned char #define uint unsigned int #define DisPort P0 #define KeyPort P2 sbit weiL = P1^0; sbit duanL = P1^1; sbit BeepPin = P3^4; uint BeepTime = 0; #define T0init_val (65536-1000) uint delTimer; uchar disBuf[8] = {10,10,10,10,10,10,10,10}; // 初始全灭 // 数码管段码表 unsigned char code disp_code[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, // 0-9 0x00, 0x40, 0x77 // 10:灭, 11:-, 12:E }; // 数码管位码表(从右到左) unsigned char code wei_code[] = { 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe }; // 计算器状态 long currentValue = 0; long storedValue = 0; uchar inputMode = 1; char operation = 0; uchar errorFlag = 0; uchar keyPressed = 0; void delayms(uint ms) { delTimer = ms; while(delTimer); } uchar GetKey(void) { uchar KeyValue = 16; KeyPort = 0x0f; if(KeyPort != 0x0f) { delayms(10); if(KeyPort != 0x0f) { // 测试列 KeyPort = 0x0f; switch(KeyPort) { case 0x0e: KeyValue = 0; break; case 0x0d: KeyValue = 1; break; case 0x0b: KeyValue = 2; break; case 0x07: KeyValue = 3; break; } // 测试行 KeyPort = 0xf0; switch(KeyPort) { case 0xe0: KeyValue += 12; break; case 0xd0: KeyValue += 8; break; case 0xb0: KeyValue += 4; break; case 0x70: break; } // 直接返回按键值,不等待释放 return KeyValue; } } return 16; } void display_update() { long val = (inputMode == 1) ? currentValue : storedValue; uchar i; if(errorFlag) { for(i=0; i<7; i++) disBuf[i] = 10; disBuf[7] = 12; // 最右侧显示E return; } if(val < 0) { val = -val; disBuf[7] = 11; // 最右侧显示负号 } else { disBuf[7] = 10; // 最右侧熄灭 } // 从右向左填充数字 for(i=0; i<7; i++) { disBuf[i] = val % 10; val /= 10; if(val == 0) break; } // 剩余位熄灭 for(i++; i<7; i++) { disBuf[i] = 10; } } void processKey(uchar key) { if(errorFlag && key != 12) return; BeepTime = 10; // 短鸣 if(key < 10) { // 数字键 if(inputMode == 1) { if(currentValue <= 9999999) { currentValue = currentValue * 10 + key; } } else { if(storedValue <= 9999999) { storedValue = storedValue * 10 + key; } } } else if(key == 10) { operation = '+'; inputMode = 2; storedValue = currentValue; currentValue = 0; } else if(key == 11) { operation = '-'; inputMode = 2; storedValue = currentValue; currentValue = 0; } else if(key == 13) { operation = '*'; inputMode = 2; storedValue = currentValue; currentValue = 0; } else if(key == 14) { operation = '/'; inputMode = 2; storedValue = currentValue; currentValue = 0; } else if(key == 15) { // 等于键 if(operation) { switch(operation) { case '+': currentValue = storedValue + currentValue; break; case '-': currentValue = storedValue - currentValue; break; case '*': currentValue = storedValue * currentValue; break; case '/': if(currentValue == 0) errorFlag = 1; else currentValue = storedValue / currentValue; break; } if(labs(currentValue) > 99999999L) errorFlag = 1; operation = 0; inputMode = 1; storedValue = 0; } } else if(key == 12) { // 清除键 currentValue = 0; storedValue = 0; inputMode = 1; operation = 0; errorFlag = 0; } display_update(); } void timer0_isr() interrupt 1 { static uchar dis_count = 0; TH0 = T0init_val >> 8; TL0 = T0init_val & 0xff; // 数码管动态扫描(从右到左) DisPort = 0x00; duanL = 1; duanL = 0; DisPort = wei_code[dis_count]; weiL = 1; weiL = 0; DisPort = disp_code[disBuf[dis_count]]; duanL = 1; duanL = 0; dis_count = (dis_count + 1) % 8; // 蜂鸣器控制 if(BeepTime) { BeepPin = ~BeepPin; BeepTime--; } else { BeepPin = 0; } if(delTimer) delTimer--; } void main() { TMOD = 0x01; TH0 = T0init_val >> 8; TL0 = T0init_val & 0xff; ET0 = 1; TR0 = 1; EA = 1; while(1) { uchar key = GetKey(); if(key < 16) { processKey(key); delayms(20); // 简单防抖 while(GetKey() == key); // 等待按键释放 } } } 以上程序是基于STM32单片机的利用矩阵键盘计算的程序,但是以上程序没有办法完成算数的计算,请在以上程序的基础上进行修改,
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值