Count ways to reach the n’th stair
PROBLEM :
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter).
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, an integer N will be given.
Output:
Print number of possible ways to reach Nth stair. Answer your output % 10^9+7.
Constraints:
1<=T<=1000
1<=N<=100
Example:
Input:
3
4
10
24
Output:
5
89
75025
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
long long Count_nth_stair(long long ) ;
int main()
{
int t ;
long long no ;
cin>>t ;
while(t--)
{
cin>>no ;
no=Count_nth_stair(no) ;
cout<<no<<endl ;
}
return 0;
}
long long Count_nth_stair(long long no)
{
long long arr[no+1] ;
int i ;
arr[0]=1 ;
arr[1]=1 ;
for(i=2;i<=no;i++)
arr[i]=(arr[i-1]+arr[i-2])%1000000007 ;
return arr[no] ;
}
---------------------------------------------------------------------------------
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter).
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, an integer N will be given.
Output:
Print number of possible ways to reach Nth stair. Answer your output % 10^9+7.
Constraints:
1<=T<=1000
1<=N<=100
Example:
Input:
3
4
10
24
Output:
5
89
75025
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
long long Count_nth_stair(long long ) ;
int main()
{
int t ;
long long no ;
cin>>t ;
while(t--)
{
cin>>no ;
no=Count_nth_stair(no) ;
cout<<no<<endl ;
}
return 0;
}
long long Count_nth_stair(long long no)
{
long long arr[no+1] ;
int i ;
arr[0]=1 ;
arr[1]=1 ;
for(i=2;i<=no;i++)
arr[i]=(arr[i-1]+arr[i-2])%1000000007 ;
return arr[no] ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment