(一)一个简单的取物游戏的算法
来源:互联网 发布:横截面数据举例 编辑:程序博客网 时间:2024/05/30 22:51
博主是一个java菜鸟,偶然间朋友推荐博主找leetcode.com上的算法从简单到难没事就写几个,所以博主决定每一到两天贴出一个自己写的算法code,因为是新手,代码一定显得拙劣,希望能和大神以及编程爱好者们相互交流,共同提高。以下是今天的算法和code:
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
(简单讲就是一个两个人玩的取物游戏,每个人能取1到3个数,取到最后一个数的人赢)public class Nim {public boolean canWinNim(int num) {// num是石头的数量ArrayList array = new ArrayList();for (int i = 0; i < num; i++) {array.add(i);}System.out.println("石头的数量为:" + num + "\n");int m = (num - 1) % 4;// 4个一组以后还剩的数量为mint p = (num - 1) / 4;// 4个一组一共有p组if (m == 3) {System.out.println("后手赢,稳的一逼!!\n");} else {System.out.println("先手赢,稳的一逼!!\n");}if (num > 4) {System.out.println("需要取得数为:");for (int o = 0; o < p; o++) {System.out.print((m + 1 + 4 * o) + " ");}return true;}return false;}}
1 0
- (一)一个简单的取物游戏的算法
- 一个简单的扑克牌游戏(一)
- 游戏编程笔记-起步(一)一个简单的游戏-贪吃蛇
- 一个递归实现的取数游戏
- 1403 一个新的取石子游戏
- 博弈论 ,一个新的取石子游戏
- 一个关于“取数问题”的游戏
- 一个简单的flash游戏(截图)
- 一个简单的游戏源代码
- 一个简单的游戏(1)
- 一个简单的游戏(2)
- 一个简单的游戏(3)
- 一个简单的游戏源代码
- 一个简单的游戏源代码
- 一个简单的游戏源代码
- 一个简单的游戏源代码
- 一个简单的游戏源代码
- 一个简单的游戏源代码
- CocosGUI---Menu
- Java 实现单例
- A
- C++学习笔记第一篇
- BZOJ 1198: [HNOI2006]军机调度
- (一)一个简单的取物游戏的算法
- CodeForces 637B Chat Order
- 使用ViewGroup实现RecyclerView
- Cocos场景切换和屏幕截取
- cv机枪手opencv3——同时显示多个摄像头
- 感悟
- Openstack Icehouse Spice配置
- Eclipse项目导入AndroidStudio出现中文乱码
- 数据结构之--series,DataFrame.use python and pandas for data mining