Count number of hops
PROBLEM :
Frog steps either 1, 2 or 3 steps to go to top. In how many ways it reaches the top?
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N. Where is the number of steps it has to hop.
Output:
Print the number of ways.
Constraints:
1 = T = 50
1 = N = 50
Example:
Input:
2
1
5
Output:
1
13
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long number_of_hops(long long ) ;
int main()
{
int t ;
long long no ;
cin>>t ;
while(t--)
{
cin>>no ;
no=number_of_hops(no) ;
cout<<no<<endl ;
}
return 0;
}
long long number_of_hops(long long no)
{
no=no+1 ;
long long arr[no] ;
int i ;
arr[0]=1 ;
arr[1]=1 ;
arr[2]=2 ;
if(no>=3)
for(i=3;i<=no;i++)
arr[i]=arr[i-1]+arr[i-2]+arr[i-3] ;
return arr[no-1] ;
}
---------------------------------------------------------------------------------
Frog steps either 1, 2 or 3 steps to go to top. In how many ways it reaches the top?
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N. Where is the number of steps it has to hop.
Output:
Print the number of ways.
Constraints:
1 = T = 50
1 = N = 50
Example:
Input:
2
1
5
Output:
1
13
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long number_of_hops(long long ) ;
int main()
{
int t ;
long long no ;
cin>>t ;
while(t--)
{
cin>>no ;
no=number_of_hops(no) ;
cout<<no<<endl ;
}
return 0;
}
long long number_of_hops(long long no)
{
no=no+1 ;
long long arr[no] ;
int i ;
arr[0]=1 ;
arr[1]=1 ;
arr[2]=2 ;
if(no>=3)
for(i=3;i<=no;i++)
arr[i]=arr[i-1]+arr[i-2]+arr[i-3] ;
return arr[no-1] ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment