【问题标题】:Get min and max integer value containing X number of digits获取包含 X 位数的最小和最大整数值
【发布时间】:2020-09-27 19:46:47
【问题描述】:

生成包含x 位数的最低和最高整数值的最佳方法是什么?

例如:

  • x = 1:最小值 = 0,最大值 = 9
  • x = 2:最小值 = 10,最大值 = 99
  • x = 3:最小值 = 100,最大值 = 999
  • x = 4:最小值 = 1000,最大值 = 9999

我觉得这应该是一件很容易完成的事情,但我很难理解它背后的数学原理。

这是我到目前为止生成 ma​​x 值 (based on this answer) 的结果:

public static int MaxIntWithXDigits(this int x)
{
    if (x == 0) throw new ArgumentException(nameof(x), "An integer cannot contain zero digits.");

    try
    {
        return Convert.ToInt32(Math.Pow(10, x) -1);
    }
    catch 
    {
        throw new InvalidOperationException($"A number with {x} digits cannot be represented as an integer.");
    }
}

如果有人可以帮助生成 min 值或建议对上述代码进行改进,我将不胜感激。

编辑

我快到了 - 这似乎适用于所有情况,除非x = 1(并且最小值预期为 0)

public static int MinIntWithXDigits(this int x)
{
    if (x == 0) throw new ArgumentException(nameof(x), "An integer cannot contain zero digits.");

    x -= 1;

    try
    {
        return Convert.ToInt32(Math.Pow(10, x));
    }
    catch
    {
        throw new InvalidOperationException($"A number with {x} digits cannot be represented as an integer.");
    }
}

【问题讨论】:

  • 你的问题太宽泛了。您具体遇到了什么问题?您是否想到给定位数的 最小值 值与少一位加一位的 最大值 值相同?
  • 它很笨,但它有效:parseInt(x > 1 ? "1" + "0".repeat(x-1) : "0")

标签: c# math numbers number-formatting


【解决方案1】:

Fox 任意 x>1,最小值为 10x-1。例如:

  • 如果 x = 2,则 min=102-1 = 101 = 10
  • 如果 x = 3,则 min=103-1 = 102 = 100
  • 如果 x = 4,则 min=104-1 = 103 = 1000

对于 x=1,由于它是唯一不是一个后跟一系列零的值,因此您需要特殊处理。

public static int MinIntWithXDigits(this int x)
{
    if (x == 0) 
    {
        throw new ArgumentException(nameof(x), "An integer cannot contain zero digits.");
    }

    if (x == 1) {
        return 0;
    }

    try
    {
        return Convert.ToInt32(Math.Pow(10, x - 1));
    }
    catch 
    {
        throw new InvalidOperationException($"A number with {x} digits cannot be represented as an integer.");
    }
}

旁注:
如果结果被限制为 整数而不是 非负 整数,那么最小的一位整数将为 1,并且通用公式也适用于这种情况.

【讨论】:

    【解决方案2】:

    你可以试试这样的:

    // let's return min and max in one go as a value tuple
    public static (int min, int max) MaxIntWithXDigits(this int x) {
      if (x <= 0 || x > 9)
        throw new ArgumentOutOfRangeException(nameof(x));
    
      int min = (int)Math.Pow(10, x - 1); 
    
      return (min == 1 ? 0 : min, min * 10 - 1); 
    }
    

    演示:

    var result = Enumerable
      .Range(1, 9)
      .Select(x => $"{x} : {MaxIntWithXDigits(x).min} .. {MaxIntWithXDigits(x).max}"); 
    
    Console.Write(string.Join(Environment.NewLine, result));
    

    结果:

     1 : 0 .. 9
     2 : 10 .. 99
     3 : 100 .. 999
     4 : 1000 .. 9999
     5 : 10000 .. 99999
     6 : 100000 .. 999999
     7 : 1000000 .. 9999999
     8 : 10000000 .. 99999999
     9 : 100000000 .. 999999999
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 2021-11-27
      • 2012-11-20
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多