用户操作
[即时聊天] [发私信] [加为好友]
潘宇光ID:superdullwolf
32739次访问,排名3596好友34人,关注者266
超级大笨狼,每天要自强。
superdullwolf的文章
原创 53 篇
翻译 0 篇
转载 0 篇
评论 68 篇
最近评论
superdullwolf:Grayeyeso_o

http://www.dullwolf.cn/Idiom.rar
可以下载了
QQ18795736:老大讲的 COOL
Grayeyeso_o:顶了,顺便求份成语接龙的代码,wxj1987@yahoo.com.cn,地址打不开,谢过了
devoc:很厉害哦你
devoc:晕,怎么点三下就发了三了次哦。麻烦lz删两个哈~
文章分类
收藏
    相册
    偶的照片
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 传说中百度的试题,用C#做二进制运算得到2.5亿数字中不重复数字数的O(n)算法收藏

    新一篇: 广度优先,模拟爬虫,嗅探URL  | 旧一篇: 成功研究出多边形的碰撞检测算法

     using System;
    using System.IO;
    using System.Collections;


    namespace bucketSort
    {
       
    class Program
        {
           
    public const int maxCount = 25 * 1000 * 10000;//2.5亿不算大,还没超过整数范围呢
            public const string FileName = @"c:\test.txt";

           
    public static void Main(string[] args)
            {

               
    //写文件,用来实验的;写一遍就可以了;         
               
    //writeFile();
               
    //Console.ReadLine();
               
    //return;

                FileStream  FileReader
    = new FileStream(FileName, FileMode.Open);        

               
    //循环读入全部正个整数,每次读1个整数,产生1个bit,读到结尾标记就不读了;

               
    int index = 0;
               
    //创建两个够长的BitArray    
               
    //发现了.NET一个Bug,就是BitArray并不是可以按照int.MaxValue构造,所以限制了一下数字范围,期待微软能修改这个Bug。
               
    //即使是在最大整数范围内,-2,147,483,648 到 2,147,483,647是4亿个Bit,1亿多字节, New这两个容器应该是2个100多M,在600M范围内。
               
    //一个数组表示有没有这个数字

                BitArray CountorSingle
    = new BitArray(250000000 + 1);

               
    //第二个数组表示是否多次出现这个数字
                BitArray CountorMore = new BitArray(250000000 + 1);

               
    //循环检测,复杂度是O(N)
                while (true)
                {
                   
    byte[] tempByte = new byte[4];
                    index
    += FileReader.Read(tempByte, 0, 4);
                   
    int tempInt = System.BitConverter.ToInt32(tempByte, 0);

                   
    if (tempInt == -1)
                    {
                       
    break;
                    }
                   
    else
                    {
                       
    if (CountorSingle.Get(tempInt))
                        {
                           
    //出现多次,设置这个值,复杂度是O(1)
                            CountorMore.Set(tempInt, true);
                        }
                       
    else
                        {
                           
    ////出现一次,设置这个值,复杂度是O(1)
                            CountorSingle.Set(tempInt, true);
                        }
                    }
                }
                FileReader.Close();

               
    //从CountorSingle中排除CountorMore,采用逻辑Xor运算,复杂度是O(n)          
               
    //Xor运算如果两者值相同,为False,1,0=1 ; 0,1=1 ; 1,1=0 ; 0,0=0  前边已经排除了0,1的可能性。             
                CountorSingle = CountorSingle.Xor(CountorMore);//复杂度是O(n)           
               
    //再读负数那个文件。
               
               
    //关键时刻到来了
                int sum = 0;

               
    //复杂度是O(n)
                foreach (bool isExists in CountorSingle)
                {
                   
    if (isExists)
                    {
                        sum
    ++;
                    }
                }

                Console.WriteLine(
    "全部计算复杂度是O(n),结果是:" + sum);
                Console.ReadLine();
               
    //几分钟后,看到屏幕上显示:全部计算复杂度是O(n),结果是:91836109


            }
           
    public static long getIndex(int x)
            {
               
    //int.MinValue的下标位于0,负整数n的下标是0-(long)int.MinValue;
               
    //0的下标位于1-(long)int.MinValue,正整数n的下标是n+1-(long)int.MinValue;
                if (x >= 0)
                {
                   
    return (x + 1 - (long)int.MinValue) / 4;
                }
               
    else
                {
                   
    return (0 - x) / 4;
                }
            }
           
    public static void writeFile()
            {
               
    //写文件,用来实验的;写一遍就可以了,正数和零写到正数文件里,负数写到负数文件里。
               
    //写2个文件是因为.NET的BitArray类只有构造函数是int的,如果都写一起,那么就超出了处理范围。


                FileStream FileWriter
    = new FileStream(FileName, FileMode.OpenOrCreate);

                System.Random rand
    = new Random();
               
    int i = 0;
               
    for (; i < maxCount; i++)
                {
                   
    //写入2.5亿个整数,一个整数是4个Byte[],随机在250000000范围内;
                   
    //发现了.NET一个Bug,就是BitArray并不是可以按照int.MaxValue构造。
                   
    //所以就只好限制在250000000内了,先只考虑正数和0,
                   
    //负数可以另外改一下,放另外文件里计算。
                    int tempInt = rand.Next(0, 250000000);
                    FileWriter.Write(System.BitConverter.GetBytes(tempInt),
    0, 4);

                }
               
    //结尾写一个-1做标记。
                FileWriter.Write(System.BitConverter.GetBytes(-1), 0, 4);
                FileWriter.Close();
              
    //靠,970多兆的文件。实验完赶紧删除。
                Console.WriteLine("写文件完毕!");
            }


        }
    }



    发表于 @ 2008年07月22日 17:20:00|评论(loading...)|收藏

    新一篇: 广度优先,模拟爬虫,嗅探URL  | 旧一篇: 成功研究出多边形的碰撞检测算法

    评论:没有评论。

    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © 潘宇光