巴什博奕(Bash Game)只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个。最后取光者得胜。

显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走多少个,后取者都能够一次拿走剩余的物品,后者取胜。因此我们发现了如何取胜的 法则:如果n=(m+1)r+s,(r为任意自然数,s≤m),那么先取者要拿走s=n/(m+1)个物品,如果后取者拿走k(≤m)个,那么先取者再拿走m+1-k个, 结果剩下(m+1)(r-1)个,以后保持这样的取法,那么先取者肯定获胜。总之,要保持给对手留下(m+1)的倍数,就能最后获胜。

#include <cstdlib>
#include <iostream> using namespace std; int main(int argc, char *argv[])
{
int total, n, m; cout << "Welcome to NIM. Pick a staring total: ";
cin >> total;
cout << "请输入你想减去值的范围最大值。";
while (total <= )
{
cout << "请输入一个大于0的数: ";
cin >> total;
if (total > )
{
break;
} }
while (true)
{
//通过判断,做出最好的选择,并打印出结果。 if ((total % m+ == m))
{
total = total - ;
cout << "I am subtacting 2." << endl;
}
else
{
total --;
cout << "I am subtacting 1." << endl;
}
cout << "New total is " << total << endl;
if (total == )
{
cout << "I win!" << endl;
break;
} //获得用户的输入,必须是1或者2; cout << "Enter num to subtract (1 or 2):";
cin >> n;
while (n < || n > )
{
cout << "Input must be 1 or 2." << endl;
cout << "Re-enter. ";
cin >> n;
}
total = total - n;
cout << "New total is " << total << endl;
if (total == )
{
cout << "You win." << endl;
break;
}
} system("PAUSE");
return EXIT_SUCCESS;
}

最新文章

  1. Evolutionary Computing: [reading notes]On the Life-Long Learning Capabilities of a NELLI*: A Hyper-Heuristic Optimisation System
  2. OC-id、构造方法
  3. JS-011-颜色进制转换(RGB转16进制;16进制转RGB)
  4. 使用Jsoup 抓取页面的数据
  5. ubuntu 新系统需要做的事
  6. MySQL中UNION和UNION ALL的使用
  7. 【不积跬步,无以致千里】五个常用的Linux监控脚本代码
  8. Spring 系列: Spring 框架简介(转载)
  9. js判断终端是手机还是电脑
  10. JAVA实例变量的初始化过程
  11. wampserver一系列问题总结
  12. 越狱解决iphone4s外放无声音
  13. mysql+mycat实现读写分离
  14. mac 回车键、空格键失灵(非物理原因)解决方法
  15. 128、View 绘制流程 &amp; 自定义View
  16. css垂直居中方法
  17. Win10系列:UWP界面布局基础12
  18. 【Linux】 解决报错: ImportError: libSM.so.6: cannot open shared object file: No such file or directory
  19. 解析Array.prototype.slice.call(arguments)
  20. mongoDB系列之(二):mongoDB 副本集

热门文章

  1. angular.js 中同步视图和模型数据双向绑定,$watch $digest $apply 机制
  2. python3.3使用tkinter实现猜数字游戏代码
  3. angular学习笔记(二十二)-$http.post
  4. sqlite遇到database is locked问题的完美解决
  5. 原声js实现addClass removeClass toggleClass效果
  6. ubuntu16.04中文乱码解决方案
  7. ehcache OR Memcache
  8. ORM框架为什么不流行了
  9. MySQL Daemon failed to start
  10. JS中onclick事件传参