【问题标题】:How to add or subtract within a certain range如何在一定范围内加减
【发布时间】:2020-11-03 11:13:20
【问题描述】:

我正在尝试添加数字,但将其保持在一个范围内。如果它超过或低于边界,它将绕回另一个边界。

例子:

最少:10 最大:50

  • 20 + 10 = 30
  • 20 + 30 = 50(或 0 idrk)
  • 20 + 31 = 1
  • 40 + 20 = 10
  • 40 + 70 = 10
  • 40 - 10 = 30
  • 10 - 30 = 30
  • 10 - 20 = 40
  • 10 - 70 = 40

我正在寻找两种功能,一种是加法,一种是减法。我真的不在乎哪种语言,但首选 python 或 java。感谢您的帮助!

【问题讨论】:

  • 您需要在 python 中使用 abs% ( modulo ) 来实现这一点。到目前为止,您尝试过什么?
  • 为什么 20+31 = 1 ??不应该是 19 岁吗?
  • 这就是 (a + b ) % 50 或 (a - b) % 50,其中 a, b 是表达式中的数字,运算是 '+' 或 '-' .
  • 我认为这个问题有点棘手,因为If it oversteps or understeps the boundary it will circle back to the other boundary. 在上面的例子中我仍然不确定20+31 = 1 的情况:/
  • 如果范围的最小值是10,最大值是50,那么20 + 31不应该是10吗?不应该是 40 + 20 = 50 + 1 + 9 = 19?

标签: java python math add subtraction


【解决方案1】:

恐怕我们必须为您的问题添加更具体的信息:

  • 边界是包含还是排除?
  • 我们谈论的是整数还是浮点数?

如果您想将例程用作库函数用于多种用途,最好将一端定义为包含,另一端定义为排他。

以下(不完整的)Java 程序显示了解决方案的方向以及边界问题:每当遇到边界时,它就会翻转到值框架的另一端:

public class FramedAddSub {

int min;
int max;
int value;

public FramedAddSub(int min, int max, int value) {
    this.min = min;
    this.max = max;
    this.value = value;
}

public FramedAddSub add(int toAdd) {
    final int diff = max - min;

    if (toAdd >= 0) {
        // step 1: norm to zero
        value -= min;

        // step 2: rest of division
        value = (value + toAdd) % diff;

        // step 3: re-norm to old offset
        value += min;
    } else {
        // step 1: norm to zero from other end
        value -= max;
        
        // step 2:
        value = (value + toAdd) % diff;
        
        // step 3: re-norm back
        value += max;
    }

    return this;
}

public static void main(String[] args) {
    FramedAddSub test = new FramedAddSub(20, 50, 20);

    System.out.println("start: " + test.value);
    test.add(10);
    System.out.println("+10: " + test.value);
    test.add(20);
    System.out.println("+20: " + test.value);
    test.add(1);
    System.out.println("+1: " + test.value);
    test.add(30);
    System.out.println("+30 should just turn around circle: " + test.value);
    test.add(-1);
    System.out.println("-1: " + test.value);
    test.add(-1);
    System.out.println("-1: " + test.value);
    test.add(-30);
    System.out.println("-30 should just turn around circle: " + test.value);

 }
}

【讨论】:

    【解决方案2】:

    在 python 中试试这个:

    ans = abs(a + b) % 50 
    

    如果要进行减法运算,则需要将 ab 作为负数传递。

    【讨论】:

      【解决方案3】:
      # define the range
      start = 0
      end = 93
      
      #  this function accepts positive and negative numbers in x
      def mover(current_place, x):
          locate = current_place + x
          locate = locate % (end-start) # if x > the range
          if locate > end:
              locate = locate - end
          if locate < start:
              locate = locate + end
          return locate
      
      
      if __name__ == "__main__":
          current_place = 50
          print(current_place) #50
          current_place = mover(current_place, 10)
          print(current_place) #60
          current_place = mover(current_place, -20)
          print(current_place) #40
          current_place = mover(current_place, -50)
          print(current_place) #83
          current_place = mover(current_place, 220)
          print(current_place) #24
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        相关资源
        最近更新 更多