有17个人围成一圈(编号0-16),从第0号的人开始由1报数,凡报到3的倍数的人离开圈子.然后再数下去,直到最后只剩下一个人为止.问此人原来是几号?

解决方案 »

  1.   

    #include "stdio.h"
    main()
    {
    int i,test,p[17],head;
    for(i=0;i<16;i++)
    p[i]=i+1;
    p[16]=0;
    test=0;
    while(test!=p[test])
    {
    for(i=1;i<3;i++)
    {
    head=test;
    test=p[test];
    }
    p[head]=p[test];
    test=p[head];
    }
    printf("\n%5d",test);
    解释下
      

  2.   

    public class KillMan { /**
     * @param args
     */
    public static void main(String[] args) {
    int liveNum=new KillMan().liveNum(17);
    System.out.println("此人原来是"+liveNum+"号");
    }

    private int liveNum(int total){
    String temp="";
    //随便写的,大家不要笑
    for(int i=0;i<total;i++){
    temp+=""+(char)(i+30000);
    }
    String tt=temp;
    while(temp.length()>2){
    temp=temp.substring(3)+temp.substring(0,2);
    }
    temp=temp.substring(1);
    return tt.indexOf(temp);
    }}不知以前有人这样写不?  搞笑的.......
      

  3.   

    public class Count3Quit {
    public static int peopleCount = 17;
    public static void main(String[] args) {
    int[] a = new int[peopleCount];
    for(int i=0; i<a.length; i++) {
    a[i] = 1;
    }

    int leftCount = peopleCount;
    int countNum = 0;
    int index = 0;

    while(leftCount != 1) {
    if(a[index] == 1) {
    countNum ++;
    if(countNum == 3) {
    countNum = 0;
    a[index] = 0;
    leftCount --;
    }
    }

    index ++;
    if(index == a.length) {
    index = 0;
    }
    }

    for(int i=0; i<a.length; i++) {
    if(a[i] == 1) {
    System.out.println("剩下的人的编号为 : " + i);
    }
    }
    }
    }
      

  4.   

    这个不就和约瑟夫问题一样的挖
    public class run{
     int boy_num=17;
     newlist nl=new newlist(boy_num);
     int way=3;
        public run(){
         for(int i=0;i<boy_num;i++){
           for(int j=0;j<way;j++){
              nl.next(); 
          }
          nl.gandiao();
          }
       }
         public static void main(String [] ars){
         new run(); 
    }
    }
    class newlist{
    int[] boys;
    int num;
    int location;
    public newlist(int a){
     location=-1;//因为如果是0的话数的第一为就是1了 为了数的第一位是0所以这里location初始为-1
     num=a;
     boys=new int[num];
     for(int i=0;i<num;i++)
        boys[i]=1;
    }
    public void next(){
       if(location==(boys.length-1)){
          location=0;
          if(boys[location]==0){
          next();
        }
       }else {location++;
         if(boys[location]==0){
          next();
         }
        }
    }
    public void gandiao(){
          boys[location]=0;
          System.out.println("我挂了,我市"+(location+1)+"号");}
    }