简体   繁体   English

比较存储在字符串中的非常大的数字

[英]Compare very large numbers stored in string

What is the best way to compare two very large numbers contained in string literals? 比较字符串文字中包含的两个非常大的数字的最佳方法是什么?

For example I want to compare the followings: "90000000000000000000000000000000000000000000000000000000000000000000000000001" "100000000000000000000000000000000000000000000000000000000000000000000000000009" 例如,我想比较以下内容:“ 90000000000000000000000000000000000000000000000000000000000000000000000000000000000000001”“ 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”

or 要么

"0000000000111111111111111111111111111111111111111111111111111111111111111111" "0000001111111111111111111111111111111111111111111111111111111111111111111111" “” 0000000000111111111111111111111111111111111111111111111111111111111111111111111“”“ 0000001111111111111111111111111111111111111111111111111111111111111111111111111111”

In both cases obviously the second one is greater, but how could I find it out efficiently without iterating on elements? 在这两种情况下,第二种显然都更大,但是如何在不迭代元素的情况下有效地找到它呢?

I would personally take the simplest approach: use BigInteger to parse both values, and compare those results. 我个人将采用最简单的方法:使用BigInteger解析两个值,然后比较这些结果。 That wouldn't be terribly efficient, but it would be very simple - and then you could benchmark to see whether it's fast enough . 不是非常有效,但是会非常简单-然后您可以进行基准测试以查看它是否足够快。

Otherwise, you could find the effective length by ignoring leading zeroes - and if one number is longer than the other, then that's all you need to know. 否则,您可以通过忽略前导零来找到有效长度-如果一个数字比另一个数字长,那么这就是您需要知道的。 Or write a method to get the "effective" digit of a string which may be shorter, returning 0 if necessary, and then compare from the longer string's length downwards until one string gives a bigger value. 或编写一种方法来获取字符串的“有效”数字,该数字可能会更短,如果需要,则返回0,然后从较长的字符串的长度开始向下进行比较,直到一个字符串的值更大。 Something like: 就像是:

// Return the digit as a char to avoid bothering to convert digits to their
// numeric values.
private char GetEffectiveDigit(string text, int digitNumber)
{
    int index = text.Length - digitNumber;
    return index < 0 ? '0' : text[index];
}

private int CompareNumbers(string x, string y)
{
    for (int i = int.Max(x.Length, y.Length); i >= 0; i--)
    {
        char xc = GetEffectiveDigit(x, i);
        char yc = GetEffectiveDigit(y, i);
        int comparison = xc.CompareTo(yc);
        if (comparison != 0)
        {
            return comparison;
        }
    }
    return 0;
}

Note that this doesn't check that it's a valid number at all, and it definitely doesn't attempt to handle negative numbers. 请注意,这根本不会检查它是否是有效数字,并且绝对不会尝试处理负数。

If by comparison you mean boolean check, this will work: 相比之下,如果您是布尔检查,那么它将起作用:

public class Program
{
    static void Main(string[] args)
    {
        string a = "0000000090000000000000000000000000000000000000000000000000000000000000000000000000001";

        string b = "000100000000000000000000000000000000000000000000000000000000000000000000000000009";

        Console.WriteLine(FirstIsBigger(a, b));

        Console.ReadLine();
    }

    static bool FirstIsBigger(string first, string second)
    {
        first = first.TrimStart('0');
        second = second.TrimStart('0');
        if (first.Length > second.Length)
        {
            return true;
        }
        else if (second.Length == first.Length)
        {
            for (int i = 0; i < first.Length; i++)
            {
                double x = char.GetNumericValue(first[i]);
                double y = char.GetNumericValue(second[i]);
                if (x > y) return true;
                else if (x == y) continue;
                else return false;
            }
        }

        return false;
    }
}

Here is another solution: 这是另一种解决方案:

    public static int CompareNumbers(string x, string y)
    {
        if (x.Length > y.Length) y = y.PadLeft(x.Length, '0');
        else if (y.Length > x.Length) x = x.PadLeft(y.Length, '0');

        for (int i = 0; i < x.Length; i++)
        {
            if (x[i] < y[i]) return -1;
            if (x[i] > y[i]) return 1;
        }
        return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM