四十个亿个整数,快速判断是否存在
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个数字是否存在。