Get all variants?..
Moderatoren: crack, Marwin, Krüsty
Hi everybody!
Can you help me?
I need to write such thing:
for example, I jave array:
1,2,3,4....n.
I need to get ALL variants of these numbers, that in summing = n.
Sorry, for my bad English - Im Russian, Ill try to explain:
for example, I have:
1,2,3
So all variants are:
1,1,1 (1+1+1=3)
1,2 (1+2=3)
2+1 (2+1=3)
3 (3=3)
Please, help me to get such variants..
I need this part of code to be written on Java or C#.NET.
Can you help me?
I need to write such thing:
for example, I jave array:
1,2,3,4....n.
I need to get ALL variants of these numbers, that in summing = n.
Sorry, for my bad English - Im Russian, Ill try to explain:
for example, I have:
1,2,3
So all variants are:
1,1,1 (1+1+1=3)
1,2 (1+2=3)
2+1 (2+1=3)
3 (3=3)
Please, help me to get such variants..
I need this part of code to be written on Java or C#.NET.
If the ship don't know where to go - it has no fair wind...
- Marwin
- Moderator
- Beiträge: 307
- Registriert: Donnerstag 8. Mai 2003, 21:19
- Wohnort: Seelow, Deutschland
- Kontaktdaten:
Hi Ignat, I think that works how you described it (I also attached the program).
Regards, Marwin
---
Btw: Ignat, today I added 2-Way Mergesort, Straight 2-Way Mergesort, Natural 2-Way Mergesort and Radix Exchange-Sort. I think itll be ready soon.
Code: Alles auswählen
public class GetAllVariants {
public static void main (String[] args) {
GetAllVariants(3);
}
public static void GetAllVariants (int N) {
GetAllVariants(0, N, new String());
}
public static void GetAllVariants (int sum, int N, String numbers) {
if ( sum == N ) return;
for ( int number = 1; number <= N; number++ ) {
if ( (sum + number) <= N ) {
numbers = numbers.concat(String.valueOf(number) + "+");
GetAllVariants(sum + number, N, numbers);
if ( (sum + number) == N ) {
System.out.println(numbers.substring(0, numbers.length() - 1) + " = " + String.valueOf(N));
}
numbers = numbers.substring(0, numbers.length() - 2);
}
}
}
}
---
Btw: Ignat, today I added 2-Way Mergesort, Straight 2-Way Mergesort, Natural 2-Way Mergesort and Radix Exchange-Sort. I think itll be ready soon.
Re: Get all variants?..
Hi,
This is exceptionally decent and wonderful post.
I am very happy joine this forum.
I like it exceptionally much....!!!!
Thanks alot...

This is exceptionally decent and wonderful post.
I am very happy joine this forum.
I like it exceptionally much....!!!!
Thanks alot...


Sumer