【问题标题】:method that return digit by index按索引返回数字的方法
【发布时间】:2018-12-21 11:11:04
【问题描述】:

我想编写一个方法,返回数字 (num) 的数字 (place),而 不使用 数组仅循环和条件。

例如:

  1. 对于输入num = 17489place = 1,该方法返回9
  2. 但是如果索引不存在在likenum = 17489,place = 6返回-1

如果我设置i = 0,我的代码中的某些内容不起作用 case 1) 工作但 2) 否 如果我设置i = 1 case 2) 工作但 1) 否 请帮忙找出问题所在

    public static int Digit_by_place(int num, int place)
    {
        int digit = 0, i = 0;

        // the lop run until the place in the 
        while (num > 0 && i <= place)number
        {
            digit = num % 10;
            num = num / 10;
            i++;

            // if there is no didgit in the input place return -1
            if (i < place && num == 0)
            {
                return -1;
            }

            // return the last digit from left (the bigest)
            if (num == 0 && digit > 0 && i == place)
            {
                return digit;
            }
        }

        return digit;
    }

【问题讨论】:

  • 对英语有基本的了解会很有帮助。真的很难理解什么有效,什么无效。除此之外,请明确“不工作”的实际含义。
  • @HimBromBeere 在我的代码中如果我设置 i=1 并输入 1-num 长度之间的任何索引它可以工作问题是如果我输入 iindex>num 长度例如 14879,5 返回 1 原因就是num 中的最后一位,但如果我输入 14879,6 它仍然返回 1 insted of -1

标签: c# digits


【解决方案1】:

我们可以利用int 最多可以有10 数字这一事实。

代码:

public static int Digit_by_place(int num, int place) {
  if (place <= 0 || place > 10)   // place must be in [1..10] range
    return -1;
  else if (num == 0)              // special case num == 0
    return place == 1 ? 0 : -1;

  for (int i = 1; i < place; ++i)  
    num /= 10;

  return num == 0 ? -1 : Math.Abs(num % 10);
}

演示:

using System.Linq;
...

Tuple<int, int>[] tests = new Tuple<int, int>[] {
  Tuple.Create( 17489,  1),
  Tuple.Create( 17489,  2),
  Tuple.Create( 17489,  3),
  Tuple.Create( 17489,  4),
  Tuple.Create( 17489,  5),
  Tuple.Create( 17489,  6),
  Tuple.Create(-17489,  1),
  Tuple.Create(-17489,  6),
  Tuple.Create( 17489,  0),
  Tuple.Create( 17489, -1),
  Tuple.Create(-17489, -1),
  Tuple.Create( 17489,  5),
  Tuple.Create(-17489,  5),
  Tuple.Create(     0,  1),
  Tuple.Create(     0,  4),
};

var report = string.Join(Environment.NewLine, tests
  .Select(test => 
     $"Digit_by_place({test.Item1,6}, {test.Item2,2}) = {Digit_by_place(test.Item1, test.Item2),2}"));

Console.Write(report);

结果:

Digit_by_place( 17489,  1) =  9
Digit_by_place( 17489,  2) =  8
Digit_by_place( 17489,  3) =  4
Digit_by_place( 17489,  4) =  7
Digit_by_place( 17489,  5) =  1
Digit_by_place( 17489,  6) = -1
Digit_by_place(-17489,  1) =  9
Digit_by_place(-17489,  6) = -1
Digit_by_place( 17489,  0) = -1
Digit_by_place( 17489, -1) = -1
Digit_by_place(-17489, -1) = -1
Digit_by_place( 17489,  5) =  1
Digit_by_place(-17489,  5) =  1
Digit_by_place(     0,  1) =  0
Digit_by_place(     0,  4) = -1

【讨论】:

  • 不应该Digit_by_place(-17489, 1) = 9返回-9吗?
  • @Magnus:我假设数字在 [0..9] 中,否则现在代表 error-1 将与来自 Digit_by_place(-17489, 5)-1 冲突。如果需要有符号位,我们应该删除Math.Abs
  • 是的,我想如果 -1 用于超出范围是有道理的
【解决方案2】:

只使用数学。

public int GetNumber(int number, int position)
{
    if(number == 0 && position == 1) 
       return 0;
    if(position <= 0 || position > (int)(Math.Log10(number = Math.Abs(number)) + 1)) 
       return -1;
    return (int)((number % (Math.Pow(10, position))) / (Math.Pow(10, position - 1)));
}

【讨论】:

  • 现在是正确的,+1;但是在解决纯integer 任务时使用double 数学方法(Math.Log10Math.Pow)恕我直言,这是一个过度。
【解决方案3】:
for(int i=0; i<place; i++)
{
       num = num / 10;
       if (num <= 0)
           return -1;
}
return num % 10;

如果你从 0 开始计数,这应该可以工作

【讨论】:

  • 这不好用,如果我输入 17489,1 则返回 8,而不是返回 9
  • 如果num非负数 整数(如123)条件if (num &lt; 0)永远满足
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
  • 2021-01-30
  • 2015-01-29
  • 2011-02-25
  • 1970-01-01
相关资源
最近更新 更多