分糖果游戏
2014-12-05 21:24
549 查看
十个小孩围成一圈分糖果,老师分给第一个小孩10块,第二小孩2块,第三个小孩8块,第四个小孩22块,第五个小孩16块,第六个小孩4块,第七个小孩10块,第八个小孩6块,第九个小孩14块,第十个小孩20块。然后所有的小孩同时将自已手中的糖分一半给右边的小孩,糖块数为奇数的人可向老师要一块,问经过这样几次调整后大家手中的糖的块数都一样多?每人各有多少块糖?
![](http://s3.51cto.com/wyfs02/M00/54/6C/wKiom1SBtWax4LgkAANjtvmUcK8552.jpg)
![](http://img.baidu.com/hi/jx2/j_0003.gif)
public class test2 { public void shareSweet75() { int[] boy = { 10, 2, 8, 22, 16, 4, 10, 6, 14, 20 }; int[] copy = new int[boy.length]; int times = 0; System.out.print("Times:" + times + " "); show(boy); while (allTheSame(boy) == false) {// 不相等 System.arraycopy(boy, 0, copy, 0, 10); for (int i = 0; i <= 9; i++) { boy[i] = copy[(i - 1 + 10) % 10] / 2 + copy[i] / 2; } times++; System.out.print("Times:" + times + " "); show(boy); for (int i = 0; i <= 9; i++) { if (boy[i] % 2 == 1) boy[i]++; } if (times > 100) break; }// while System.out.print("Times:" + ++times + " "); show(boy); } /** * 判断是否全相等,只有一个数返回 */ public boolean allTheSame(int[] a) { if (a == null || a.length == 0) { return false; } if (a.length == 1) return true; for (int i = 1; i < a.length; i++) { if (a[0] != a[i]) { return false; } } return true; } /** * 显示数组的值 */ public void show(int[] a) { if (a == null) System.out.println("Array = null"); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void main(String[] args) { // TODO Auto-generated method stub new test2().shareSweet75(); } }运行结果为
![](http://s3.51cto.com/wyfs02/M00/54/6C/wKiom1SBtWax4LgkAANjtvmUcK8552.jpg)
![](http://img.baidu.com/hi/jx2/j_0003.gif)
相关文章推荐
- P1196吃糖果游戏解题报告
- vijos1196吃糖果游戏
- 吃糖果游戏(tyvj 1567)
- 如何快速做一个糖果消除类游戏(第一节)
- vijos P1196 吃糖果游戏
- python脚本打google首页的糖果篮子游戏
- BZOJ5046: 分糖果游戏
- P1567 吃糖果游戏
- Vijos P1196吃糖果游戏[组合游戏]
- [BZOJ5046]分糖果游戏
- Vijos 1196 吃糖果游戏(博弈论)
- 一个分糖果游戏的解法
- 看完仙剑,心里不太舒服,和当年玩完游戏一样............
- 游戏政治
- 据说把游戏王做成手机java客户端是可行的…
- 游戏资源网站
- 【Visual C++】游戏开发笔记三十五 站在巨人的肩膀上:游戏引擎导论
- 初级游戏外挂编程详解 windows运行原理+游戏辅助编程 游戏外挂编程
- 找对儿游戏(html5)