//约瑟夫问题9个人围成一圈.从1号开始报号,凡是数到5的人就走出圈子(出局).然后继续报号.
//试问最后一个出局的人是那一个
public class BubbleSort
{
public static void main(String[] args)
{
StringBuffer x=new StringBuffer("123456789");
int index = 0;
for(int i=0; x.length() > 1;i++)
{
if(i == 4)
{
 x.deleteCharAt(index);
    i=0;
    System.out.println(x);
}
   index++;
 if(index>=x.length() )
    index = 0;
}
System.out.println("最后一个数:" + x);
}
}
]这是我在论坛上看到的瑟夫环的解法  我的疑问是:当人数大于9是怎么解决啊

解决方案 »

  1.   

    当人数大于9时还可以用StringBuffer解决吗?
      

  2.   

    应该可以用ArrayList来作吧.
    import java.util.ArrayList;public class BubbleSort{
    public static void main(String[] args){

                ArrayList x = new ArrayList();
        for(int j = 1; j <= Integer.parseInt(args[0]); j++){
    x.add(new Integer(j));
                }
        int index = 0;
        for(int i=0; x.size() > 1;i++){
        if(i == 4){
            x.remove(index);
            i=0;
            System.out.println(x);
        }
        index++;
        if(index>=x.size() )
            index = 0;
        }
        System.out.println("the last is:" + x.get(0));
    }
    }