Reach a given score
PROBLEM :
Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find number of distinct combinations to reach the given score.
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.
Output:
Print number of ways/combinations to reach the given score.
Constraints:
1 = T = 100
1 = N = 1000
Example:
Input
3
8
20
13
Output
1
4
2
Explanation
For 1st example when n = 8
{ 3, 5 } and {5, 3} are the two possible permutations but these represent the same cobmination. Hence output is 1.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Reach_score(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
no=Reach_score(no) ;
cout<<no<<endl ;
}
return 0;
}
int Reach_score(int no)
{
int i ;
int arr[no+1]={0} ;
arr[0]=1 ;
for(i=3;i<=no;i++)
arr[i]=arr[i]+arr[i-3] ;
for(i=5;i<=no;i++)
arr[i]=arr[i]+arr[i-5] ;
for(i=10;i<=no;i++)
arr[i]=arr[i]+arr[i-10] ;
return arr[no] ;
}
---------------------------------------------------------------------------------
Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find number of distinct combinations to reach the given score.
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.
Output:
Print number of ways/combinations to reach the given score.
Constraints:
1 = T = 100
1 = N = 1000
Example:
Input
3
8
20
13
Output
1
4
2
Explanation
For 1st example when n = 8
{ 3, 5 } and {5, 3} are the two possible permutations but these represent the same cobmination. Hence output is 1.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Reach_score(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
no=Reach_score(no) ;
cout<<no<<endl ;
}
return 0;
}
int Reach_score(int no)
{
int i ;
int arr[no+1]={0} ;
arr[0]=1 ;
for(i=3;i<=no;i++)
arr[i]=arr[i]+arr[i-3] ;
for(i=5;i<=no;i++)
arr[i]=arr[i]+arr[i-5] ;
for(i=10;i<=no;i++)
arr[i]=arr[i]+arr[i-10] ;
return arr[no] ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment