We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,613 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Number decomposition

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.

3
Contributors
1
Reply
3 Months
Discussion Span
10 Months Ago
Last Updated
3
Views
modesto916
Light Poster
30 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

MonsieurPointer
Junior Poster
143 posts since Jun 2011
Reputation Points: 45
Solved Threads: 15
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0734 seconds using 2.67MB