Algorithm


Problem Name: beecrowd | 2807

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2807

Iccanobif

 

By Francisco Elio Parente Arcos Filho, UEA BR Brazil

Timelimit: 1

Iccanobif sequences are sequences where each term is always equal to the sum of the next two subsequent to it. Except for the last two terms which are always equal to 1

 

Example of an Iccanobif sequence with 10 terms: 55, 34, 21, 13, 8, 5, 3, 2, 1, 1.

 

Your task is, given an integer value, print the corresponding size Iccanobif sequence.

 

Input

 

The entry consists of a single integer N (1 ≤ N ≤ 40) representing the size of the desired Iccanobif sequence.

 

Output

 

The output consists of a single line containing the terms of the Iccanobif sequence of N size separated by a single space.

 

 

 

Exemplos de Entrada Exemplos de Saída

3

2 1 1

 

 

 

5

5 3 2 1 1

 

 

 

10

55 34 21 13 8 5 3 2 1 1

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
int main(){
   int n,a=1,b=0,temp,i,len;
   scanf("%d",&n);
   int array[n];
   for(i = 0; i  <  n; i++){
    array[i]=a;
    temp=a+b;
    b=a;
    a=temp;
   }
   for(i = n-1; i >= 0; i--){
        if(i==0){
          printf("%d",array[i]);
        }else{
          printf("%d ",array[i]);
        }
   }
   printf("\n");
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
3

Output

x
+
cmd
2 1 1

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
#include <stack>

using namespace std;

int main()
{
    int n, a=1, b=1, c=1;
    cin >> n;
    if (n < 3)
    {
        if (n==1)
            cout << 1 << endl;
        else
            cout << 1 << " " << 1 << endl;
        return 0;
    }
    else
    {
        stack < int>s;
        s.push(1);
        s.push(1);
        for (int i = 2; i  <  n; ++i)
        {
            c=a+b;
            a=b;
            b=c;
            s.push(c);
        }
        while (!s.empty())
        {
            cout << s.top();
            if (s.size()!=1)
                cout << " ";
            s.pop();
        }
        cout << endl;
        return 0;
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
2 1 1

Output

x
+
cmd
3

#3 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
const size = parseInt(prompt());
var fib = [1, 1];

for (let i = 1; i  <  size; i++) {
  fib.push(fib[i] + fib[i - 1]);
}

fib.length = size;

console.log(fib.reverse().join(" "));
Copy The Code & Try With Live Editor

Input

x
+
cmd
3

Output

x
+
cmd
2 1 1

#4 Code Example with Python Programming

Code - Python Programming


fib = [1, 1]
for x in range(2, 41):
    fib.append(fib[x-1]+fib[x-2])
fib.sort(reverse=True)
n = int(input())
palavra = ''
for x in fib[41-n:]:
    palavra += str(x) + ' '

palavra = palavra[:-1]
print(palavra)

Copy The Code & Try With Live Editor

Input

x
+
cmd
3

Output

x
+
cmd
2 1 1
Advertisements

Demonstration


Previous
#2802 Beecrowd Online Judge Solution 2802 Dividing Circles Solution in C, C++, Java, Js and Python
Next
#2808 Beecrowd Online Judge Solution 2808 Knights Again Solution in C, C++, Java, Js and Python