Thursday, June 26, 2014

TopCoder High School SRM 22 Level one - 250 pt - Fibonacci Sequence solution

TopCoder High School SRM 22 Level one - 250 pt - Fibonacci Sequence statement: http://community.topcoder.com/stat?c=problem_statement&pm=7262&rd=10074

TopCoder High School SRM 22 Level one - 250 pt - Fibonacci Sequence editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm22

TopCoder High School SRM 22 Level one - 250 pt - Fibonacci Sequence solution: 

#include <iostream>
using namespace std;

class FibonacciSequence {
    int fib(int n) {
        if(n==1 || n==2) return 1;
        return fib(n-1) + fib(n-2);
    }
    public: int numFibs(int a, int b) {
        int arr[1500]={0}, cnt=0;
        for(int i=1; i<=16; i++) arr[fib(i)]++;
        for(int i=a; i<=b; i++) if(arr[i]>0) cnt++;
        return cnt;
    }
};

No comments:

Post a Comment