c# 设n个人围坐在一个圆桌周围

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3
{
    /*class Shu
    {
        public void make(int[]   b,int   n,int   s,int   m)  
        {
            int[] b2=new int[n];
            for (int i = 0; i <= n; i++)
            {
                b2[i] = b[i];
                if (((i+s) % m) == 0)
                {
                    Console.WriteLine(b[i]);
                    n--;
                    for (int j = i; j < n; j++)
                    {
                        b2[j] = b[j + 1];
                        //Console.Write(b2[i]);
                    }
                }
               
            }
            if (n==1) return;
            make(b2, n, s, m); 
        } 
    }*/

    public class Josephas
    {
        //总人数为n , 从第s人开始计数,以m为单位循环记数出列
        public int [ ] Jose(int n, int s, int m)   
       {         
            int i, j, k = 0;
            //count数组存储按出列顺序的数据,以当结果返回
            int[] count = new int [ n+1];   
 
            //s数组存储初始数据
            int[] sum = new int [ n+1];   
 
            //对数组sum赋初值,第一个人序号为0,第二人为1,依此下去
            for (i = 0; i <=n; i++)   
            {
                sum[i] = i;
            }
 
            //按出列次序依次存于数组count中
            for (i = n; i >= 2; i--)     
            {
                s = (s + m - 1) % i;
                if (s == 0)
                    s = i;
                count[k] = sum[s];
                //Console.Write(count[k]);
                k++;
                for (j = s + 1; j <= i; j++)
                    sum[j - 1] = sum[j];
            }            
            count[k]=sum[1];
            //结果返回
            return count;   
       }
    }

    class Program
    {
        static void Main(string[] args)
        {
            /*int n=0,s=0,m=0;
            n = Convert.ToInt32(Console.ReadLine());
            s = Convert.ToInt32(Console.ReadLine());
            m = Convert.ToInt32(Console.ReadLine());
            int[] x=new int[n];
            Shu sh = new Shu();
            for (int i = 0; i < n; i++) x[i] = i+1;
            sh.make(x, n, s, m);
            Console.ReadLine();*/

            int n = 0, s = 0, m = 0;
            Console.Write("请输入总人数n:");
            n = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入开始报数位置s:");
            s = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入第几个出列数m:");
            m = Convert.ToInt32(Console.ReadLine());
            int[] count=new int[n];
            Josephas jp = new Josephas();
            count=jp.Jose(n, s, m);
                for (int i = 0; i <n; i++)
            {
                Console.Write(count[i]+"  ");
            }
            Console.ReadLine();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值