圆桌问题代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
 
namespace SF_Test1
{
    class Program
    {
        static void Main(string[] args) {
            Console.WriteLine("请输入初始数据:"); string str = Console.ReadLine(); string[] strArray = str.Split(',');//原始数据        
            Console.WriteLine("请输入起始位置:"); string startT = Console.ReadLine(); int start=int.Parse(startT);
            Console.WriteLine("请输入初始位移:"); string mT = Console.ReadLine();int m=int.Parse(mT);
 
            string[] result = solveDate(strArray,start,m);
            Console.WriteLine(string.Join(",", result));
            Console.ReadLine();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="number">总数据量</param>
        /// <param name="start">开始位置</param>
        /// <param name="m">初始位移量</param>
        /// <returns></returns>
        public static int getPeople(int number, int start, int m) {
            int result ;
            result = (start + m) % number;           
            return result;
        }
        /// <summary>
        /// 移出出局数据
        /// </summary>
        /// <param name="source">原始数据</param>
        /// <param name="index">是下标加1</param>
        /// <returns></returns>
        public static  string[] deleteOneNumber(string[] source, int index) {
            string[] result = new string[source.Length-1];
            if (index != 0) {
                for (int i = 0; i < result.Length; i++) {
                    if (i < index - 1) {
                        result[i] = source[i];
                    }
                    else {
                        result[i] = source[i + 1];
                    }
                }
            }
            else {
                for (int i = 0; i < result.Length; i++) {                   
                        result[i] = source[i];                   
                }
            }
            return result;
        }
        /// <summary>
        /// 数据处理
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="m"></param>
        /// <returns></returns>
        public static string[] solveDate(string[] source,int start,int m) {
            string[] strArray = source;
            int number = strArray.Length;
            string[] resultT = new string[strArray.Length];
            while (strArray.Length > 1) {
                int resultIndex = number - strArray.Length;
                int getIndex = getPeople(strArray.Length, start, m);
                if (getIndex != 0) {
                    resultT[resultIndex] = strArray[getIndex - 1];
                }
                else {
                    resultT[resultIndex] = strArray[strArray.Length - 1];
                }
                strArray = deleteOneNumber(strArray, getIndex);
                if (getIndex > 0) {
                    start = getIndex;
                }
                else {
                    start = 1;
                }
                m = m + 1;
            }
            resultT[number - 1] = strArray[0];
            return resultT;
        }
    }
}
 

转载于:https://www.cnblogs.com/softwaregirl/archive/2012/09/27/RoundTable.html

猜你喜欢

转载自blog.csdn.net/weixin_34258078/article/details/94703286