当前位置: 首页 > 知识库问答 >
问题:

获取包含X位数的最小和最大整数值

仰钧
2023-03-14

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

例如:

  • x=1:Min=0,Max=9

我觉得这应该是一件非常容易完成的事情,但是我正在努力理解它背后的数学。

以下是迄今为止我得到的生成最大值的方法(基于此答案):

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.");
    }
}

如果有人能帮助生成最小值,或者对上面的代码提出改进建议,我将不胜感激。

编辑

我就快到了——这似乎适用于所有情况,除非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.");
    }
}

共有2个答案

杜祺
2023-03-14

你可以试试这样:

// 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
杭镜
2023-03-14

Fox any x

  • 如果x=2,最小值=102-1=101=10

对于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,通用公式也将适用于这种情况。

 类似资料:
  • 问题内容: 我正在寻找python中整数的最小值和最大值。例如,在Java中,我们有和。python中是否有类似的东西? 问题答案: Python 3 在Python 3中,此问题不适用。普通int类型是无界的。 但是,你实际上可能正在寻找有关当前解释器的字长的信息,在大多数情况下,该信息将与机器的字长相同。该信息在Python 3中仍以形式提供,这是一个有符号的单词可以表示的最大值。等效地,它是

  • 问题内容: 我有一个像这样的数组: 我需要提取最小和最大的权重值。在这个例子中 $ min_value = 175 $ max_value = 200 有什么帮助吗?谢谢 ! 问题答案: 选项1. 首先,您映射该数组以获取这些数字(而不是全部详细信息): 然后得到最小和最大: 选项2。 (仅当您没有PHP 5.5或更高版本时)与选项1相同,但要选择值,请使用: 选项3。 选项4。 如果您只需要一个

  • 本文向大家介绍C#获取数组中最大最小值的方法,包括了C#获取数组中最大最小值的方法的使用技巧和注意事项,需要的朋友参考一下 根据下面函数获取数组中最大最小值即可。调用时候直接传数组范围一个float类型的变量  

  • 问题内容: 最近在一次采访中有人问我这个问题。 给定以下代码,静态整数的最小和最大可能值是多少? 我告诉他们,最大值将为25(在没有竞争条件的情况下),而最小值将为5(在每次迭代时所有线程之间的竞争条件的情况下)。 但是面试官说,最小值甚至可以低于5。这 怎么可能? 问题答案: 我声称最小值可能是2。 这样做的关键是的非原子性,即它是读和写,它们之间可能有其他操作。 调用线程T1..T5: T1读

  • 在C语言中,整数(对于32位机器)是32位,其范围为-32,768到+32,767。在Java中,整数(长)也是32位,但范围从-2,147,483,648到+2,147,483,647。 我不明白Java中的范围是怎么不同的,尽管位数是一样的。有人能解释一下吗?