Sunday, 27 March 2016

Solution of URI 1151 :: Easy Fibonacci


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#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;
}

3 comments:

  1. #include
    int main()
    {
    int a=0,b=1,c=0,n,i;
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {

    printf("%d ",a);
    c=a+b;
    a=b;
    b=c;
    }
    printf("%d\n",a);
    }

    ReplyDelete
  2. Obrigado.

    Consegui resolver com sua ajuda em C#.

    using System;

    namespace uri1151FibonacciFacil
    {
    class Program
    {
    static void Main(string[] args)
    {
    int n, i, f0 = 0, f1 = 1, f = 0;

    n = int.Parse(Console.ReadLine());

    for (i = 1; i < n; i++)
    {
    if (i % 2 == 1)
    {
    Console.Write(f + " ");
    f = f0 + f1;
    f0 = f;
    }
    else if (i == 2)
    {
    Console.Write(f + " ");
    }
    else if (i % 2 == 0)
    {
    Console.Write(f + " ");
    f = f0 + f1;
    f1 = f;
    }
    }
    Console.WriteLine(f);
    }
    }
    }

    ReplyDelete
  3. i didnt really understand the code

    ReplyDelete

Note: only a member of this blog may post a comment.