百度空间 | 百度首页 
 
查看文章
 
关于约瑟夫环(c#解)
2008-01-03 14:01

    class Program
    {
        static void Main(string[] args)
        {
            int r = new Cysf(10,5).GetLast();
            Console.WriteLine(r);
        }
    }

    class Cysf
    {
        private int _max = 0;

        private int _n = 0;

        public Cysf(int max,int n)
        {
            _max = max;
            _n = n;
        }

        public int GetLast()
        {
            int m = 0;
            ArrayList list = new ArrayList();
            InitList(ref list, _max);
            while (list.Count > 1)
            {
                for (int i = 0; i < list.Count;)
                {
                    if (++m % _n == 0)
                    {
                        Console.WriteLine(Convert.ToInt32(list[i]));
                        list.RemoveAt(i);
                    }
                    else
                    {
                        ++i;
                    }
                }
            }
            return Convert.ToInt32(list[0]);
        }

        private void InitList(ref ArrayList list, int max)
        {
            for (int i = 1; i <= max; i++)
            {
                list.Add(i);
            }
        }
    }


类别:Public | 添加到搜藏 | 浏览() | 评论 (1)
 
最近读者:
 
网友评论:
1
2009-09-06 20:47 | 回复
不管能不能用 先顶了
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu