Good day,

My Data Structure teacher asked me to do a list of exercises, and one of the questions consists of writing a program to solve the numbers decomposition problem, for example:

If the user types the number 5:

5
4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1

I need to use the list data structure to do that, anyway my problem isn't with the data structure, my problem is with the algorithm. Does anyone know a simple way to do this?

Regards.

Member Avatar for MonsieurPointer

There is a pattern. The first column is initially the entered number (5), subtracted by 1. Hence you have 4 in the first column. The difference between the first number and the initial number is 1, since 5 - 4 = 1. The first number is again decreased by 1, resulting in 3. 5 - 3 = 2. 2 by itself can be expressed by 1 + 1, that'S why the 3rd row has 3 + 1 + 1. I think you can take it from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.