约瑟夫环

 #region 约瑟夫环

        static void SuesfuRing()

        {

            Console.WriteLine("请输入几个人");

            int n = Convert.ToInt32(Console.ReadLine());



            int[] a = new int[n];

            int i = 0;

            for (i = 0; i < n; ++i )

            {

                a[i] = i + 1;

                Console.WriteLine("  " + a[i]);

            }

            Console.WriteLine("数到几被踢出");

            int m = Convert.ToInt32(Console.ReadLine());



            int k = 0;//记得报数

            int c = n;//剩余人数

            i = 0;

            while ( c > 1 )

            {

                if ( 0 != a[i] )

                {

                    k++;

                    

                }

                if ( k == m )

                {

                    Console.WriteLine("被踢出" + a[i]);

                    a[i] = 0;

                    k = 0;

                    c--;

                }

                i++;

                i %= n;

            }

            for ( i = 0; i < n; ++i )

            {

                if ( 0 != a[i] )

                {

                    Console.WriteLine("最后一个人是:" + a[i]);

                }

            }

            

        }

        #endregion

 

你可能感兴趣的:(约瑟夫环)