• 闲来无事,写个算法关于11000放在含有1001个元素。。。


    1-1000放在含有1001个元素的数组中,只有唯一的一个元素值重复,其它均只出现
    一次。每个数组元素只能访问一次,设计一个算法,将它找出来;不用辅助存储空
    间,能否设计一个算法实现?

    class Program
        {
            static void Main(string[] args)
            {
                int[] list = new int[101];
                Random r = new Random();
                int a= r.Next(1, 100);
                bool b = false;

                for (int i = 0; i < 101; i++)
                {

                    if (!b)
                        list[i] = i + 1;
                    else
                        list[i] = i;

                    if (a == i + 1)
                    {
                        list[i + 1] = a;

                        b = true;
                    }


                }
                Console.WriteLine("The Random number : "+a.ToString());
                int left = 0, right = 100;

                int posion;

                posion = (left + right) / 2;
                int count = 0;

                while (right - left > 1)
                {

                  
                    if (list[posion] == posion)
                    {
                        right = posion;
                    }              
                    else
                    {
                        left = posion;
                      
                    }
                    posion = (left + right) / 2;

                   ++count;
                  
                }
           
                Console.WriteLine("At the " + count.ToString() + "th time found out the  Random number : " + list[posion].ToString() + " ; And his posion is " + posion.ToString());
                Console.Read();

            }
        }


  • 相关阅读:
    UVALive
    UVALive
    UVA
    UVALive
    BZOJ3597 SCOI2014方伯伯运椰子(分数规划+spfa)
    BZOJ3456 城市规划(多项式求逆)
    BZOJ4182 Shopping(点分治+树形dp)
    BZOJ4383 Pustynia(线段树+拓扑排序)
    BZOJ4445 SCOI2015小凸想跑步(半平面交)
    BZOJ5311 贞鱼(动态规划+wqs二分+决策单调性)
  • 原文地址:https://www.cnblogs.com/zlddtt/p/1528491.html
Copyright © 2020-2023  润新知