RANDOMIZED ALGORITHMS 4

QUESTION

You can win three kinds of basketball points, 1 point, 2 points, and 3 points. Given a total score n, print out all the combination to compose n.\n\nExamples:\nFor n = 1, the program should print following:\n1\n\nFor n = 2, the program should print following:\n1 1\n2\n\nFor n = 3, the program should print following:\n1 1 1\n1 2\n2 1\n3\n\nFor n = 4, the program should print following:\n1 1 1 1\n1 1 2\n1 2 1\n1 3\n2 1 1\n2 2\n3 1\n\nand so on.

ANSWER

#define MAX_POINT 3
#define ARR_SIZE 100
#include<stdio.h>
void printArray(int arr[], int arr_size);
 void printCompositions(int n,int i)
{
     static int arr[ARR_SIZE];
     if(n==0)
    {
        printArray(arr,i);
    }
    else if(n>0)
    {
        int k; 
        for(k=1;k<=MAX_POINT;k++)
        {
        arr[i]=k;
        printCompositions(n-k,i+1);
        }
    }
}
void printArray(int arr[], int arr_size)
{
    int i;
    for(i=0;i<arr_size;i++)
    {
      if(i!=arr_size-1)
        printf("%d ",arr[i]);
      else
      {
        printf("%d",arr[i]);
        printf("\n");
      }
    }
  //  printf("\n");
}
int main()
{
    int n;// = 5;
     scanf("%d",&n);
    printf("Differnt compositions formed by 1, 2 and 3 of %d are",n);
  printf("\n");
  
 // Differnt compositions formed by 1, 2 and 3 of 4 are
    
    printCompositions(n,0);
    return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.