Saturday, 18 July 2015

BOMARBLE

Standard

D - Playing with Marbles

Link to the question : BOMARBLE 

HINT :

Simple formula to get accepted. We need to solve it using recursion. Observe the sample input and the output and try to get a recursive relation among them. If still stuck, check the source code.

RECOMMENDED QUESTION :

Try your hands on this ad-hoc question .

SOURCE CODE :

#include<stdio.h>
#include<math.h>

long long int rec(int m)
{
    if(m==1)
        return 5;
    else if(m==2)
        return 12;
    else
        return rec(m-1) + 10 + (m-3)*3;
}

int main()
{
    int n,x=1;;
    long long int s;


    while(x!=0)
    {
           scanf("%d",&n);
           x=n;
           if(x!=0)
           {
               s = rec(x);
               printf("%lld\n",s);
           }


    }
    return 0;

}

0 comments:

Post a Comment