0

例如,我有一个参考号a = 15b= 3.

  • 如果x=2f(a,b,x) = 1因为如果将 15 分成 3 部分,则数字 2 在第一部分中。
  • 如果x=7f(a,b,x) = 2因为如果将 15 分成 3 部分,则数字 7 在第二部分中。
  • 如果x=15f(a,b,x) = 3因为如果将 15 分成 3 部分,则数字 15 在第三部分中。
  • 如果 x<0 或 >15,则结果与我无关。

有没有这样的内置功能?

4

2 回答 2

7

I can't think of a single built-in function which would do exactly that. It is however not difficult to write one:

def f(a, b, x):
  return (x - 1) * b // a + 1

for i in range(1, 16):
  print i, f(15, 3, i)

This prints out

1 1
2 1
3 1
4 1
5 1
6 2
7 2
8 2
9 2
10 2
11 3
12 3
13 3
14 3
15 3

(It is not entirely clear from the question how, and if, x=0 needs to be handled; this answer considers it to be outside the valid range.)

于 2014-03-10T19:54:25.890 回答
1

不,没有这样的内置功能。但是,您当然可以自己编写。

于 2014-03-10T19:52:57.750 回答