URI Online Judge | 1151
Fibonacci Fácil
Adaptado por Neilor Tonin, URI
Brasil
Timelimit: 1
Brasil
A
seguinte sequência de números 0 1 1 2 3 5 8 13 21... é conhecida como
série de Fibonacci. Nessa sequência, cada número, depois dos 2
primeiros, é igual à soma dos 2 anteriores. Escreva um algoritmo que
leia um inteiro N (N < 46) e mostre os N primeiros números dessa
série.
Entrada
O arquivo de entrada contém um valor inteiro N (0 < N < 46).
Saída
Os valores devem ser mostrados na mesma linha, separados por um espaço em branco. Não deve haver espaço após o último valor.
URI Online Judge | 1151
Easy Fibonacci
Adapted by Neilor Tonin, URI
Brazil
Timelimit: 1
Brazil
The
following sequence of numbers 0 1 1 2 3 5 8 13 21 ... is known as the
Fibonacci Sequence. Thereafter, each number after the first 2 is equal
to the sum of the previous two numbers. Write an algorithm that reads an
integer N (N < 46) and that print the first N numbers of this
sequence.
Input
The input file contains an integer number N (0 < N < 46).
Output
The numbers should be printed on the same line, separated by a blank space. There is no space after the last number.
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int first = 0;
int second = 1;
int fibonacci;
int n;
cin >> n;
cout << "0";
n--;
while (n--)
{
cout << " " << second;
fibonacci = second;
second+= first;
first = fibonacci;
}
cout << '\n';
return 0;
}
0 Comentários