URI Online Judge | 1151
Fibonacci Fácil
Adaptado por Neilor Tonin, URI
Brasil
Timelimit: 1 
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.
#include <stdio.h>
int main()
{
int x,y, a=0,b=1,c=0;
scanf("%d", &x);
for(y=1; y<x; y++)
{
if(y%2==1)
{
printf("%d ",c);
c=a+b;
a=c;
}
else if(y==2)
printf("%d ",c);
else if(y%2==0)
{
printf("%d ",c);
c=a+b;
b=c;
}
}
printf("%d\n",c);
return 0;
}
URI Online Judge | 1151
Easy Fibonacci
Adapted by Neilor Tonin, URI
Brazil
Timelimit: 1
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 <stdio.h>
int main()
{
int x,y, a=0,b=1,c=0;
scanf("%d", &x);
for(y=1; y<x; y++)
{
if(y%2==1)
{
printf("%d ",c);
c=a+b;
a=c;
}
else if(y==2)
printf("%d ",c);
else if(y%2==0)
{
printf("%d ",c);
c=a+b;
b=c;
}
}
printf("%d\n",c);
return 0;
}
0 Comentários