zoukankan      html  css  js  c++  java
  • 四十个亿个整数,快速判断是否存在

     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个数字是否存在。

  • 相关阅读:
    HarmonyOS Java UI之AdaptiveBoxLayout布局示例
    【鸿蒙开发板试用报告】OneNet平台+开发板实时监控温湿度(一)
    安装了瑞友天翼4.0后出现了远程桌面无法连接的问题
    CISVC.EXE的资源占用
    Delphi如何在窗体标题栏添加按钮
    Delphi中捕捉窗体的最小化、最大化、还原消息
    打印机任务无法删除
    Delphi创建一个虚幻的层窗口(Win2000/XP)
    工资年结时提示“上年数据已经结转”
    Delphi中如何控制其他程序窗体上的窗口控件
  • 原文地址:https://www.cnblogs.com/zquan/p/9813865.html
Copyright © 2011-2022 走看看