反转百分比

Tom*_*len 5 javascript math

我有一个百分比,范围从50%到0%.

我需要镜像值,所以:

0% now equals 50%
1% = 49%
25% = 25%
48% = 2%
50% = 0%
Run Code Online (Sandbox Code Playgroud)

等等

谢谢你的帮助!

pol*_*nts 13

看起来你想要定义一个这样的函数:

(x)      f(x)
 0        50
 1        49
 2        48
 :         :
48         2
49         1
50         0
Run Code Online (Sandbox Code Playgroud)

然后功能很简单:

f(x) = 50 - x
Run Code Online (Sandbox Code Playgroud)

更一般地说,如果x介于low和之间high,则:

f(x) = (high + low) - x
Run Code Online (Sandbox Code Playgroud)

其他感兴趣的功能

以下是一些其他常见功能:

(x)    f(x)___
 0       0    |
 1       0    3
 2       0 ___|
 3       1    |
 4       1    3     f(x) = x / 3
 5       1 ___|           where / is integer division
 6       2    |
 7       2    3
 :       : ___|

(x)    f(x)___
 0       0    |
 1       1    3
 2       2 ___|
 3       0    |
 4       1    3     f(x) = x % 3
 5       2 ___|           where % is integer remainder
 6       0    |
 7       1    3
 :       : ___|
Run Code Online (Sandbox Code Playgroud)

在索引二维表时,有时会将上述两种结合起来:

  ______4 columns______
 /                     \
 _______________________     (x)   row(x)   col(x)
|     |     |     |     |     0      0        0
|  0  |  1  |  2  |  3  |     1      0        1
|_____|_____|_____|_____|     2      0        2      row(x) = x / 4
|     |     |     |     |     3      0        3      col(x) = x % 4
|  4  |  5  |  6  |  7  |     4      1        0
|_____|_____|_____|_____|     5      1        1      x = row(x) * 4 + col(x)
|     |     |     |           6      1        2
|  8  |  9  | ... |           7      1        3
|_____|_____|_____|           :      :        :
Run Code Online (Sandbox Code Playgroud)

  • 提升你并不是因为你能够直接回答问题,而是因为一般情况下解决问题的方法很好. (3认同)

Mar*_*ers 12

您可以使用j = max_i - i + min_i两个常量min_i和max_i是范围的下限和上限.

如果我总是在0到50之间那么你就可以写j = 50 - i.