zoukankan      html  css  js  c++  java
  • 闲来无事,写个算法关于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();

            }
        }


  • 相关阅读:
    面向对象三大特性之封装
    基本数据类型和引用数据类型
    面向对象三大特性之继承
    多表连接查询
    MySQL模糊查询
    MySQL数据查询入门
    Matlab 之 find()函数
    Matlab 之 字符串数组查找
    Matlab 之 数据元素访问
    让WIN10输入法变回传统模式
  • 原文地址:https://www.cnblogs.com/zlddtt/p/1528491.html
Copyright © 2011-2022 走看看