#include<iostream>
int* fib(int);
int main()
{
int count;
std::cout<<"enter number upto which fibonacci series is to be printed"<<std::endl;
std::cin>>count;
int *p=new int[count];
p=fib(count);
int i;
for(i<0;i<=count;i++)
std::cout<<p[i]<<std::endl;
return 0;
}
int* fib(int d)
{
int *ar=new int[d];
int p=-1,q=1,r;
int j;
for(j=0;j<=d;j++)
{
r=p+q;
ar[j]=r;
p=q;
q=r;
}
return ar;
delete ar;
}
Run Code Online (Sandbox Code Playgroud)
这个程序是打印斐波那契系列与系列中的给定计数.请分享一些想法,我怎么能转换这个程序,找到两个数字之间的斐波纳契系列.
我正在使用任务并行库(TPL)来计算斐波纳契数.计划如下:
public static int Fib(int n)
{
if (n <= 1)
{
return n;
}
Task<int> task = Task.Factory.StartNew<int>(() => Fib(n - 1));
var p = Fib(n - 2);
return task.Result + p;
}
public static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
Console.WriteLine("Answer: " + Fib(44));
watch.Stop();
Console.WriteLine("Time: " + watch.ElapsedMilliseconds);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个程序需要很长时间才能完成.但是这个程序的序列版本(如下所示)花费不到30秒来计算第44个Fibonacci数.
public class FibTester
{
public static int Fib(int n)
{
if (n <= 1)
{
return n;
}
var q = Fib(n …Run Code Online (Sandbox Code Playgroud) 我正在尝试从Fibonacci方法中捕获值并将其连接到数组.但是,不是将循环中的每个值赋给数组,而是仅返回最后一个值.有没有办法解决?谢谢.
def fib_up_to(max)
i1, i2 = 1, 1
while i1 <= max
yield i1
i1, i2 = i2, i1+i2
end
end
def capture_arr(val)
$a = []
$a << val
end
fib_up_to(1000) do |f|
capture_arr(f)
end
p $a # => [987]
快速问题,包含的代码返回一个Fibonacci值,但它是错误的值,它关闭2,例如用户输入10,返回89,而不是34.基本上我只想返回由输入的值的Fibonacci值用户 ?
谁能发现问题?谢谢
var Newmodel = new FibonacciModel();
int a = 0;
int b = 1;
for (int i = 0; i < model.InputFromUser; i++)
{
model.FibonacciValue = a + b;
a = b;
b = model.FibonacciValue;
}
Newmodel.InputFromUser = model.InputFromUser;
Newmodel.FibonacciValue = model.FibonacciValue;
return View(Newmodel);
Run Code Online (Sandbox Code Playgroud) 我知道如何在没有递归的情况下解决这个问题,但有了它,我有一些难以理解......我需要深入解释它是如何逐行工作的
以下是问题的解决方法:
def fibo(num)
if num < 2
num
else
#this is where I get lost on the line below..
fibo(num-1) + fibo(num-2)
end
end
p fibo(6)
Run Code Online (Sandbox Code Playgroud) 我已经开始介绍F#的课程了,我在两个作业中遇到了一些麻烦.第一个让我创建两个函数,其中第一个函数接受输入并将其添加四个,第二个函数计算sqrt(x ^ 2 + y ^ 2).然后我应该为它们写两个函数表达式,但由于某种原因它给了我错误"Unexpected symbol'|' 在实施文件中".
let g = fun n -> n + 4;;
let h = fun (x,y) -> System.Math.Sqrt((x*x)+(y*y));;
let f = fun (x,n) -> float
|(n,0) -> g(n)
|(x,n) -> h(x,n);;
Run Code Online (Sandbox Code Playgroud)
第二个赋值要求我创建一个函数,它找到Fibonaccis数的序列.我写了下面的代码,但它似乎忘记了开头的0,因为输出总是n + 1而不是n.
let rec fib = function
|0 -> 0
|1 -> 1
|n -> fib(n-1) + fib(n-2)
;;
Run Code Online (Sandbox Code Playgroud)
请记住,这是第一周,所以我应该能够使用这些方法创建这些.
当我这样定义时fib(1):
def fib(n: Int) = {
lazy val fibs: Stream[BigInt] = 0 #:: 1 #:: fibs.zip(fibs.tail).map{n => n._1 + n._2}
fibs.drop(n).head
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
scala> fib(1000000)
java.lang.OutOfMemoryError: Java heap space
Run Code Online (Sandbox Code Playgroud)
另一方面,这很好(2):
def fib = {
lazy val fibs: Stream[BigInt] = 0 #:: 1 #:: fibs.zip(fibs.tail).map{n => n._1 + n._2}
fibs
}
scala> fib.drop(1000000).head
res17: BigInt = 195328212...
Run Code Online (Sandbox Code Playgroud)
此外,如果我按以下方式更改流定义,我可以drop(n).head在函数内调用,也不会得到任何错误(3):
def fib(n: Int) = {
lazy val fibs: (BigInt, BigInt) => Stream[BigInt] = (a, b) => a #:: fibs(b, …Run Code Online (Sandbox Code Playgroud) 我想计算每次调用fib(n)的次数.我写的代码如下:
#include <stdio.h>
#define N 10
int count[N + 1]; // count[n] keeps track of the number of times each fib(n) is called
int fib(int n) {
count[n]++;
if(n <= 1)
return n;
else
return fib(n - 1) + fib(n - 2);
}
int main() {
for(int i = 0; i <= N; i++) {
count[i] = 0; // initialize count to 0
}
fib(N);
// print values of count[]
for(int i = 0; i <= N; i++) {
printf("count[%d] …Run Code Online (Sandbox Code Playgroud) 我正在做我的任务的一部分,我必须这样做,如果用户在功能中输入10答案应该是
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Run Code Online (Sandbox Code Playgroud)
但我的计划结果
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Run Code Online (Sandbox Code Playgroud)
这是我的计划
def fib(n):
fibonacci = []
a = 0
b = 1
for i in range(n):
fibonacci.append(b)
a, b = b, a+b
return fibonacci
Run Code Online (Sandbox Code Playgroud) 我用C++和Pascal编写了函数,给出了第n个Fibbonacci数.正如预期的大n值(n> 92,因为甚至f(93)> 2 ^ 63 + 1)我得到了不正确的结果.
但是当我将它们与相同的n进行比较时,我会在两种语言中得到相同的结果.
这与我的想法相反,我会得到一些随机数.
我想知道为什么我得到相同的结果以及为什么我首先没有得到整数溢出.
有人可以向我解释一下吗?
码:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
long long fibo(int n){
long long a1,a2,pom;
int i=1;
a1 = 0; a2 = 1;
while(i<=n){
pom = a2;
a2 = a1 + a2;
a1 = pom;
i++;
}
return a1;
}
int main(){
int n;
cin >> n;
cout << "Function: "<< setprecision(50) << fibo(n) << endl;
}
Run Code Online (Sandbox Code Playgroud)
Program AddNums(output);
function fibo(n:integer):int64;
var
a1,a2,pom:int64;
i:integer;
begin
a1:=0;a2:=1;i:=1;
while(i<=n)do …Run Code Online (Sandbox Code Playgroud)