当前位置:编程学习 > C#/ASP.NET >>

数字从420依次往后排,但是中间缺失了几个数,如何高效的找出它们?

现在开发中遇到一个问题,就是拿号。按照正常流程是每次拿到一个420以后的数字,如果以前拿过了不能再拿。
如果是420,421,422,423
那么下一个应该拿的号是 424

如果是: 420,421,425,426,428....
如何最快的找到422?


我现在正在用的方法不知道为什么第一次执行的时候会比较卡,数据时通过ajax传输的。

public string GetIssueNO()
    {
        string sData = "-1";
        bool bol = false;
        DBManager db = new DBManager();
        if (db.Initialize())
        {
            try
            {
                int[] iArr = db.GetIssueNO();
                if (iArr.Length == 0)
                {
                    sData = "420";
                }
                else
                {
                    for (int i = 420; ; i++)
                    {
                        if (!bol)
                        {
                            for (int j = 0; j < iArr.Length; j++)
                            {
                                if (i == iArr[j])
                                {
                                    break;
                                }
                                else
                                {
                                    if (j == iArr.Length - 1)
                                    {
                                        sData = i.ToString();
                                        bol = true;
                                    }
                                    else
                                    {
                                        continue;
                                    }

                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                Log.WriteLog("GetIssueNO Exception: " + ex.Message);
            }
            db.UnInitialize();
        }
        else
        {
            Log.WriteLog("GetIssueNO Database Open Failed");
            sData = "-3";
        }
        return sData;
    }
--------------------编程问答-------------------- for (int i = 1; i < iArr.Count(); i++)
{
    if (iArr[i] != iArr[i - 1] + 1) ...
} --------------------编程问答--------------------

static int GetValue(int[] n)
{
    int low = 0;
    int high = n.Length - 1;
    while (low < high)
    {
        int mid = (low + high) / 2;
        if (n[mid] > n[0] + mid) high = mid;
        else low = mid;
        if (high - low == 1)
        {
            if (n[high] - n[low] == 1) low = high;
               break;
        }
    }
    return n[low] + 1;
}
--------------------编程问答-------------------- N1:420,421,425,426,428
N2:420,421,422,423,424,425,426,427,428
   low     mid     high
N1:420,421,425,426,428
key=N1[0]+mid --------------------编程问答--------------------
引用 1 楼 caozhy 的回复:
for (int i = 1; i < iArr.Count(); i++)
{
    if (iArr[i] != iArr[i - 1] + 1) ...
}


好吧   确实很有效率!谢谢啦 --------------------编程问答--------------------
引用 3 楼 hwenycocodq520 的回复:
N1:420,421,425,426,428
N2:420,421,422,423,424,425,426,427,428
   low     mid     high
N1:420,421,425,426,428
key=N1[0]+mid

额  总觉得这算法好眼熟啊  就是想不起来? --------------------编程问答--------------------
引用 5 楼 cjd6568358 的回复:
Quote: 引用 3 楼 hwenycocodq520 的回复:

N1:420,421,425,426,428
N2:420,421,422,423,424,425,426,427,428
   low     mid     high
N1:420,421,425,426,428
key=N1[0]+mid

额  总觉得这算法好眼熟啊  就是想不起来?

折半查找~~ --------------------编程问答--------------------
引用 6 楼 hwenycocodq520 的回复:
Quote: 引用 5 楼 cjd6568358 的回复:

Quote: 引用 3 楼 hwenycocodq520 的回复:

N1:420,421,425,426,428
N2:420,421,422,423,424,425,426,427,428
   low     mid     high
N1:420,421,425,426,428
key=N1[0]+mid

额  总觉得这算法好眼熟啊  就是想不起来?

折半查找~~

嗯嗯 谢谢啦 --------------------编程问答--------------------
引用 1 楼 caozhy 的回复:
for (int i = 1; i < iArr.Count(); i++)
{
    if (iArr[i] != iArr[i - 1] + 1) ...
}


看了一下  貌似有种情况没有考虑到?
比如 425,426,428?
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,