alex lotel

Ranch Hand

Posts: 191

posted 9 years ago

i need to constract a method which gets the two numbers

"n" and "k"

and prints all the avaible subgroops in "k size"

for example :

subsetofsize(4,2)

prints

12

41

13

32

42

34

i know i should use this recursion of subsets

i need to change it some how

i know how it works but i dont have any clue of how to change it

in order to get the resolt i was asked in the question

"n" and "k"

and prints all the avaible subgroops in "k size"

for example :

subsetofsize(4,2)

prints

12

41

13

32

42

34

i know i should use this recursion of subsets

i need to change it some how

i know how it works but i dont have any clue of how to change it

in order to get the resolt i was asked in the question

Campbell Ritchie

Sheriff

Posts: 53769

127

posted 9 years ago

More of a beginner's question.

You have been given a task to produce all the numbers which add up to

You will need a method which takes two parameters to start with.

Go through your numbers and decide what you actually want to print out.

If you have 4, 2, how do you get it to print 44, 43, 42, 41, and not 444?

Say, "It should start with two numbers, then adding one number leaves one to go."

"It should continue with one number, then adding one number leaves none to go."

"Then it stops."

Then tell us what you have got. Good luck.

[edit]Added this bit:

When I said "you have been given a task . . ." I ought to have said that what you posted finds all the numbers which add up to

[ February 15, 2008: Message edited by: Campbell Ritchie ]

You have been given a task to produce all the numbers which add up to

*n*. Somebody else has had the same problem before.You will need a method which takes two parameters to start with.

Go through your numbers and decide what you actually want to print out.

If you have 4, 2, how do you get it to print 44, 43, 42, 41, and not 444?

Say, "It should start with two numbers, then adding one number leaves one to go."

"It should continue with one number, then adding one number leaves none to go."

"Then it stops."

Then tell us what you have got. Good luck.

[edit]Added this bit:

When I said "you have been given a task . . ." I ought to have said that what you posted finds all the numbers which add up to

*n*. I tried it and it works nicely. Have you been through it to see how the recursion and iteration work?[/edit][ February 15, 2008: Message edited by: Campbell Ritchie ]

alex lotel

Ranch Hand

Posts: 191

posted 9 years ago

the output is

12

41

13

32

42

34

not

41

42

43

i cant understand what do you meen by adding one number and having one to go

there

you told me to get two numbers as the question says

i soppose that one represents the range of numbers

till what numbers to go

the other is about to how many objects to have in each group

i cand understand what i am supposed to add to

???

12

41

13

32

42

34

not

41

42

43

i cant understand what do you meen by adding one number and having one to go

there

you told me to get two numbers as the question says

i soppose that one represents the range of numbers

till what numbers to go

the other is about to how many objects to have in each group

i cand understand what i am supposed to add to

???

Campbell Ritchie

Sheriff

Posts: 53769

127

posted 9 years ago

Sorry, I confused you earlier; suggest you ask one of the moderators on this board to consider transferring this thread to beginners'.

Be grateful for small mercies; somebody else has been given "spiral filling." I think he posted on this board today. That seems far more difficult. I tried "spiral filling" and failed.

What you posted first looks the same as somebody else posted here about 6 months ago. If you give it 4 as an input it prints

4

3 1

2 2

2 1 1

1 1 1 1

which means the integers greater than 0 which add up to 4. Obviously what you are getting now is an improvement. You need to pass it two numbers, 4 and 2, where "4" means "start with the number 4," and "2" means, "print 2 numbers in the output." You seem to have got that bit to work.

Suggest you post what you have got, and tell us how it works, then see what changes are needed.

Be grateful for small mercies; somebody else has been given "spiral filling." I think he posted on this board today. That seems far more difficult. I tried "spiral filling" and failed.

What you posted first looks the same as somebody else posted here about 6 months ago. If you give it 4 as an input it prints

4

3 1

2 2

2 1 1

1 1 1 1

which means the integers greater than 0 which add up to 4. Obviously what you are getting now is an improvement. You need to pass it two numbers, 4 and 2, where "4" means "start with the number 4," and "2" means, "print 2 numbers in the output." You seem to have got that bit to work.

Suggest you post what you have got, and tell us how it works, then see what changes are needed.

alex lotel

Ranch Hand

Posts: 191

Campbell Ritchie

Sheriff

Posts: 53769

127

alex lotel

Ranch Hand

Posts: 191

posted 9 years ago

no i dont know how to change it

i set with a penn and paper

i made the steps for the input 5

i got the correct resolt

i got to the conclution that some how the "for" responsible

here for each step (each groop of numbers which sums to 5)

but i still dont have any clue about how to change it

so it will produce the resolt i want regarding this question

i set with a penn and paper

i made the steps for the input 5

i got the correct resolt

i got to the conclution that some how the "for" responsible

here for each step (each groop of numbers which sums to 5)

but i still dont have any clue about how to change it

so it will produce the resolt i want regarding this question

It is sorta covered in the JavaRanch Style Guide. |