Rolling A Dice – Finding all possibilities

Author: Gihyun Kim

Date: May 23rd, 2023

Type: Coding Exercise

Language: C++

Question: Write a program that receives natural numbers N and M as input and outputs all cases where M can be the sum of the dice rolled N times.

Input Format: The number of dice thrown N (2≤N≤8) and the sum of the eyes M (1≤M≤40) are entered in the first line.

Output Format: Output all cases where the sum of the number of times the dice is thrown equals M.
Output the smallest number first.

Input Example
3 10
Output Example
1 3 6
1 4 5
1 5 4
1 6 3
2 2 6
2 3 5

6 2 2
6 3 1
Sample input & output

My Code:

My Input & Output:

Explanation & Summary:
In this coding question, I had to create a program that outputs all the possible instances of a dice rolling N times which the values of them add up to M. N and M were recieved as inputs and I used recursion to figure out what values of a dice would end up having a value of M when rolled N times.

It was a fun problem in which I learned a lot from and I was able to practice using recursion functions again. I am currenly learning C++ outside of school and alongside learning Java, learning C++ is quite helpful as – these two languages have a lot in common.

Leave a comment