0% found this document useful (0 votes)
30 views2 pages

Assignment 0

The document describes a problem of distributing identical sweets amongst children. It asks to write functions print_a and print_b that generate and print all possible distributions of sweets satisfying different constraints and return the total number of distributions. print_a considers distributions where each child gets at least 1 sweet while print_b considers distributions where no two adjacent children can get 0 sweets.

Uploaded by

ARUOS Soura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Assignment 0

The document describes a problem of distributing identical sweets amongst children. It asks to write functions print_a and print_b that generate and print all possible distributions of sweets satisfying different constraints and return the total number of distributions. print_a considers distributions where each child gets at least 1 sweet while print_b considers distributions where no two adjacent children can get 0 sweets.

Uploaded by

ARUOS Soura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Indian Institute of Technology Kharagpur

CS29003: Algorithms Laboratory, Spring 2022

Assignment 0

2PM – 5PM 11th January, 2022

Submit a single C/C++ source file. Do not use global or static variables.

Consider the problem of distributing n (identical) sweets amongst m children. Let the character ‘S’
represent a sweet. When n = 7, line up the sweets as “SSSSSSS”. If there are m = 3 children, then we can
represent a distribution of 7 sweets amongst 3 children as “SS|S|SSSS”, where ‘|’ is a separator. The first
child gets 2 sweets, second child gets 1 sweet and the third gets 4 sweets. Another possible distrtibution is
“|SSSSSS|S” where the first child gets nothing, second child gets 6 sweets and the third gets 1. As suggested
by the example, any distribution of n sweets amongst m children can be represented as a string of length
n + m − 1 containing an arrangement of m − 1 separators and n many occurences of ‘S’ .
The input consists of 2 positive integers n and m. Your task is to generate all possible distributions
(without repetitions) of n sweets amongst m children provided some constraints are satisfied and prints the
total number of possible distributions. The output should be a list of strings (consisting of n ‘S’s and m
‘|’s), each printed in a separate line followed by the total count of strings printed. These strings represent
all possible distributions under the specified constaints.
(a) Define a function print_a that prints distributions in which each child gets atleast 1 sweet and returns
the total number of such distributions.
(b) In this part, the constraint is that no two adjacent children can get 0 sweets. Write a function print_b
that prints distributions satisfying the aforementioned constraint and returns the total number of such
combinations.
Use simple recursion for both parts. In the main() function, read n and m from the user, call print_a and
print the total number of distributions. Then call print_b and print the total number of distributions it
returns.

1
Example

n = 5
m = 4

(a)
SS|S|S|S
S|SS|S|S
S|S|SS|S
S|S|S|SS

Total number of distributions = 4

(b)
SSSS||S|
SSS|S|S|
SSS|S||S
SSS||SS|
SSS||S|S
SS|SS|S|
SS|SS||S
SS|S|SS|
SS|S|S|S
SS|S||SS
SS||SSS|
SS||SS|S
SS||S|SS
S|SSS|S|
S|SSS||S
S|SS|SS|
S|SS|S|S
S|SS||SS
S|S|SSS|
S|S|SS|S
S|S|S|SS
S|S||SSS
S||SSSS|
S||SSS|S
S||SS|SS
S||S|SSS
|SSSS|S|
|SSSS||S
|SSS|SS|
|SSS|S|S
|SSS||SS
|SS|SSS|
|SS|SS|S
|SS|S|SS
|SS||SSS
|S|SSSS|
|S|SSS|S
|S|SS|SS
|S|S|SSS
|S||SSSS

Total number of distributions = 40

You might also like