1. 程式人生 > >四十個億個整數,快速判斷是否存在

四十個億個整數,快速判斷是否存在

oui urn file line 就是 ray ole char 十個

 class Program
    {
        static void Main(string[] args)
        {
            //10
            //524156
            //5465
            //4546
            //1234567891
            //999999999
            //4
            //8
            //82
            //54654
            //564465456
            uint number;
            number 
= Convert.ToUInt32(Console.ReadLine()); for (uint i = 0; i < number; i++) { string temp = Console.ReadLine(); Control.buildByte(temp); } while (true) { uint temp = Convert.ToUInt32(Console.ReadLine());
if (Control.judgeExist(temp)) { Console.WriteLine("存在"); } else { Console.WriteLine("不存在"); } } }
 class Control
    {
        public static byte[] data = new
byte[FileContorl.LIMIT_MAX / 8 + 1]; public static bool judgeExist(uint number) { uint index_data = number / 8; uint pos_wei = number % 8; byte positioning = data[index_data]; string tempPositioning = Convert.ToString(positioning, 2); int len = tempPositioning.Length; if (tempPositioning.Length < 8) { for (int i = len; i < 8; i++) { tempPositioning = 0 + tempPositioning; } } char[] positioningChar = tempPositioning.ToCharArray(); if (positioningChar[pos_wei] == 1) return true; else return false; } public static void buildByte(string dataString) { uint temp = Convert.ToUInt32(dataString); uint index_data = temp / 8; uint pos_wei = temp % 8; byte positioning = data[index_data]; char[] positioiningChar = byteToChar(positioning); positioiningChar[pos_wei] = 1; data[index_data] = CharToByte(positioiningChar); } public static char[] byteToChar(byte positioning) { string tempPositioning = Convert.ToString(positioning, 2); int len = tempPositioning.Length; if (len < 8) { for (int i = len; i < 8; i++) { tempPositioning = 0 + tempPositioning; } } char[] positioningChar = tempPositioning.ToCharArray(); return positioningChar; } public static byte CharToByte(char[] positioningChar) { byte temp; string result = ""; for (int i = 0; i < positioningChar.Length; i++) { result += positioningChar[i]; } temp = Convert.ToByte(result,2); return temp; } }

判讀一個整數是否存在,其實就是0跟1的區別,用了位圖法,用byte存儲,一個byte一個字節,有八位,可以存儲8個數字是否存在。

四十個億個整數,快速判斷是否存在