根据给定的 TestUnit 编写函数

发布于 2024-09-02 02:40:50 字数 151 浏览 3 评论 0原文

我需要编写一个函数来满足这个输入 ->输出列表:

0 -> 0
1 -> 1
3 -> 2
4 -> 3
5 -> 5
7 -> 13
9 -> 34

f(x) = ??

I need to write a function to satisfy this input -> output list:

0 -> 0
1 -> 1
3 -> 2
4 -> 3
5 -> 5
7 -> 13
9 -> 34

f(x) = ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

衣神在巴黎 2024-09-09 02:40:50

嗯,这非常简单......如果您不担心过度拟合,那么您可以这样做:

switch(input)
     case 0: report 0
     case 1: report 1
     case 3: report 2
     ...
     default: report whatever

如果您想要一个好的解决方案,您可能需要对问题进行更多限制。您还可以考虑绘制该函数的图形,以查看是否存在任何明显的模式,或者可能显示所涉及的位。了解输入和输出是整数值还是实值(函数应该是连续的还是离散的?)也很有用。如果没有这些信息,就很难提供帮助。

编辑
显示缺失的数字有帮助:

0 -> 0
1-> 1
2-> 1
3-> 2
4-> 3
5-> 5
6-> 8
7-> 13
8-> 21
9-> 34

(斐波那契数列:f(x) = f(x-1)+f(x-2),其中 f(0)=0 且 f(1)=1)。

PS
对于此功能,动态编程或记忆化特别有用。

Well, that is incredibly easy... if you aren't concerned about over-fitting, then you can do:

switch(input)
     case 0: report 0
     case 1: report 1
     case 3: report 2
     ...
     default: report whatever

You probably need more constraints on the problem if you want a good solution. You also might consider graphing the function to see if there is any obvious pattern, or maybe show the bits involved. It would also be useful to know if the inputs and outputs are integer-valued or real-valued (is the function supposed to be continuous or discrete?). Without this information, it's a little bit hard to help.

Edit
Showing the missing numbers helps:

0 -> 0
1 -> 1
2 -> 1
3 -> 2
4 -> 3
5 -> 5
6 -> 8
7 -> 13
8 -> 21
9 -> 34

(It's the Fibonnaci numbers: f(x) = f(x-1)+f(x-2), where f(0)=0 and f(1)=1).

PS
This is a function for which dynamic programming or memoization is particularly useful.

粉红×色少女 2024-09-09 02:40:50

Eureqa 解决

round(exp(0.4807*input - 0.799938))

Solved by Eureqa

round(exp(0.4807*input - 0.799938))

萌能量女王 2024-09-09 02:40:50

我不知道这是否是家庭作业,但这是一个非常著名的序列。一个(相当大的)提示是 f(n) 取决于 f(n-1) 和 f(n-2)。如果您不关心只是被告知答案,请单击此处。实现该序列是相当简单的递归完成,但如果您遇到问题,请编辑您的帖子并指定您陷入困境的部分

I don't know if this is homework or not, but this is an extremely well-known sequence. A (rather large) hint is that f(n) is dependent on f(n-1) and f(n-2). If you're not concerned with just being told the answer, click here. Implementing the sequence is pretty trivially done recursively, but edit your post if you're having trouble with it and specify which part you're stuck on

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文