Wednesday, 31 December 2014

Problem Coin Counting

Coin Counting [C Program]

Curious Coin-Counting Problem

Given an amount A, we want you to compute the number of ways in which you
can gather A rupees if you have an infinite supply of each of C =  {1, 3, 5} valued rupee coins.

Input:
First line contains T, the number of test-cases. This is followed by T lines, where each line consists of the amount A.

Output:
For each test case, print the number of ways in which A can be formed using an infinite supply of 1, 3 and 5 rupee coins.

Sample Input:
2
5
10

Sample Output:
3
7

Constraints
T < 100
A < 101

Explanation (for first test case):
A = 5
Ways this amount can be achieved: {1,1,1,1,1}, {1,1,3}, {5}
Hence, the answer is 3.


Program:-

#include<stdio.h>


int cnt=0;

void count(int n){
 if(n==0){
  cnt++;
  return;
 }
 count(n-1);
}

void count3(int n){
 if(n==0){
  cnt++;
  return;
 }
 if(n>=3){
  count3(n-3);
 }
 count(n-1);
}

void count5(int n){
 if(n==0){
  cnt++;
  return;
 }
 if(n>=5){
  count5(n-5);
 }
 if(n>=3){
  count3(n-3);
 }
 count(n-1);
}

int main(){
 int n,T;
 int res[101],i=0;
 scanf("%d",&T);
 while(T!=0){
  scanf("%d",&n);
  count5(n);
  res[i]=cnt;
  cnt=0;
  i++;
  T--;
 }
 for(int j=0;j<i;j++){
  printf("%d\n",res[j]);
 }
 return 0;
}


------------X------------X------------X------------X------------X--------

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home