20个人围成圈数数,从1开始数,数到3推出,接着从1开始数,数到3推出,问最后一个推出的是哪个?急。。

解决方案 »

  1.   

    典型的约瑟夫问题,你可以去百度一下啊
    这有个这种问题的代码    public static int jos1( int people, int passes )
        {
            Collection<Integer> theList = new LinkedList<Integer>( );            // Construct the list
            for( int i = 1; i <= people; i++ )
                theList.add( i );            // Play the game;
            Iterator<Integer> itr = theList.iterator( );
            while( people-- != 1 )
            {
                for( int i = 0; i <= passes; i++ )
                {
                    if( !itr.hasNext( ) )
                        itr = theList.iterator( );
             
                    itr.next( );
                }               
                itr.remove( );    
            }
            
            itr = theList.iterator( );        return itr.next( );
        }在你这个问题里,people=20,pass=3