HDU 1525 - Euclid's Game ( 博弈 )
生活随笔
收集整理的這篇文章主要介紹了
HDU 1525 - Euclid's Game ( 博弈 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意
Stan和Ollie玩游戲,Stan先手。給出兩個數字,可以用大數減去小數的整數倍,要求不能減到小于0。誰先將一個數字減到0,誰獲勝。
思路
博弈
假設 a > b
比賽的時候想到了關于a - b > b ( a > 2 * b )狀態下,可以決定必勝、必敗態,故若a > 2 * b為必勝態。
若a%b == 0, a可以減去b的整數倍變為0,故誰達到這個狀態即必勝態
但是當時沒考慮周全,這里應該用循環求解,誰達到必勝態則退出循環。
AC代碼
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;int main() {int a, b;while( scanf("%d%d", &a, &b) == 2 && a && b ){if( a < b ) swap(a, b);bool first = true;for(;;){if( a % b == 0 || a > 2 * b ) break;a -= b;swap(a,b);first = !first;}if( first ) puts("Stan wins");else puts("Ollie wins");}return 0; }轉載于:https://www.cnblogs.com/JinxiSui/p/9740556.html
總結
以上是生活随笔為你收集整理的HDU 1525 - Euclid's Game ( 博弈 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shell整数型变量自增自减的实现方式(
- 下一篇: Ubuntu下添加定时任务执行php文件