一群小孩围成一个圈的经典算法
2017-05-20 07:50
585 查看
问题:
一群小孩围成一个圈,设置一个数,这个数是个小于小孩总数大于0的一个整数,从第一个小孩开始报数,当其中一个小孩报到你设置的那个数的时候离开那个圈,这样一来反复报下去,直到只剩下最后一个小孩的时候那个小孩就是胜利者,写程序来找出这个小孩。
一群小孩围成一个圈,设置一个数,这个数是个小于小孩总数大于0的一个整数,从第一个小孩开始报数,当其中一个小孩报到你设置的那个数的时候离开那个圈,这样一来反复报下去,直到只剩下最后一个小孩的时候那个小孩就是胜利者,写程序来找出这个小孩。
import java.util.ArrayList; public class Test { static int count=0; static final int target=3; public static void main(String args[]) { ArrayList<Integer> aa=new ArrayList<Integer>(); ArrayList<Integer> delaa=new ArrayList<Integer>(); for(int i=1;i<=50;i++) { aa.add(i); } System.out.println("the child count is "+50+""); System.out.println("the target is "+target+""); while(true) { if(aa.size()==1) { break; } for(Integer a:aa) { count++; if(count==target) { count=0; delaa.add(a); } } for(Integer a:delaa) { aa.remove(a); } for(int a:aa) { System.out.println("a== "+a+""); } System.out.println("############################"); } for(int a:aa) { System.out.println("the winer is "+a+""); } } }
相关文章推荐
- c语言int to string一个比较经典得算法
- 一个经典算法
- java经典算法_023给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
- Sumdiv 数论提 (数学很重要),这道题中算法变成了次要因素,数学才是最重要的,注意其中的一个经典公式(求因数和的)(很好推导)
- 一个非常经典的算法查找字符串中每个字符的个数。
- 冒泡是一个经典算法
- 白话经典算法系列之中的一个 冒泡排序的三种实现
- java经典算法_008求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字
- 挑战一个经典的算法题!(坑爹的求和)
- 白话经典算法系列之中的一个 冒泡排序的三种实现
- 一个经典的全排列算法
- 经典算法100道(3)-绘制一个圆
- 塞子算法 解决 一个经典c 程序
- java经典算法_012一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?
- LCA问题的在线算法(很经典的一个算法)
- 一个经典算法
- 一个小程序,算法很经典
- c语言经典算法之数小孩
- java经典算法_032判断一个数字是奇数还是偶数
- 一个经典算法的解法,觉得挺有意思