1,105,417 Community Members

Number decomposition

Member Avatar
modesto916
Light Poster
33 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
MonsieurPointer
Junior Poster
152 posts since Jun 2011
Reputation Points: 27 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: