将整数100 随机分成 10段

时间:2015-05-08 14:33:10   收藏:0   阅读:104
static void Main(string[] args)
        {
            int[] a=new int[101];
            Random r = new Random();
            int IntervalCount=10;             

            int count = 0;
            while(count<IntervalCount-1)
            {
                int v = r.Next(99) + 1;
                if (a[v] == 1) continue;
                else
                {
                    a[v] = 1;
                    count++;
                }
            }
            a[100] = 1;
            
            int flag = 0;
            int totalCount = 0;
            for (int i = 1; i <= 100; i++)
            {
                if (a[i] == 0) continue;
                else
                {
                    Console.WriteLine(flag.ToString()+"-"+i.ToString()+".span="+(i-flag).ToString());
                    totalCount++;

                    flag = i;
                }
            }
            Console.WriteLine("lines=>" + totalCount.ToString());
            Console.ReadKey();

            
        }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!