Pergunta de entrevista da empresa Bloomberg

Given a positive integer N, write a function to return a list of the first N fibonacci numbers in order. For example, if N=5, the output list is 0 1 1 2 3.

Respostas da entrevista

Sigiloso

19 de set. de 2016

int main() { int n = 0; cin >> n; int a = 1; int b = 1; int c = 1; if (n==1) { cout << a << endl; return 0; } cout << a << " "; while(n-- != 1) { cout << c << " "; a = b; b = c; c = a+b; } }

Sigiloso

6 de jan. de 2017

#python def fib(n): if n == 0: return 0 if n == 1: return 1 else: return fib(n-1)+fib(n-2) def runfib(N): for i in range(N): print fib(i),

Sigiloso

6 de jan. de 2017

#dynamic programming def fib2(n): r = [0,1] if n==0: return r[0] if n==1: return r[1] else: for i in range(2,n+2): r.append(r[-2]+r[-1]) if len(r)>n: break return r[-1] def runfib2(N): for i in range(N): print fib2(i),

Sigiloso

19 de jul. de 2017

#include using namespace std; int main() { int a,b,c, n; cin >> c; a = b = n = 0; while(n