Get all variants?..

Moderatoren: crack, Krüsty, Marwin

Beitragvon Ignat » Freitag 16. April 2004, 12:44

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.
If the ship don't know where to go - it has no fair wind...
Ignat
Alter Hase
 
Beiträge: 80
Registriert: Samstag 2. August 2003, 11:13

Beitragvon Marwin » Freitag 16. April 2004, 21:54

Shall the result of this operations always be the largest number of the array?
Are the numbers in the array sorted in an ascending order or are they unsorted?

Marwin
Benutzeravatar
Marwin
Moderator
 
Beiträge: 307
Registriert: Donnerstag 8. Mai 2003, 21:19
Wohnort: Seelow, Deutschland

Beitragvon Ignat » Samstag 17. April 2004, 20:26

I need all variants! The position of numbers is very importatnt - for example, 1+2 != 2+1. Its not the same things!
Hm.. Its not array.. I want to write function like
GetAllVariants(int n) , where n the value, like "3" in my example..
If the ship don't know where to go - it has no fair wind...
Ignat
Alter Hase
 
Beiträge: 80
Registriert: Samstag 2. August 2003, 11:13

Beitragvon Marwin » Sonntag 18. April 2004, 05:31

Hi Ignat, I think that works how you described it (I also attached the program).

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);

      }

    }
    
  }
}


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.
Benutzeravatar
Marwin
Moderator
 
Beiträge: 307
Registriert: Donnerstag 8. Mai 2003, 21:19
Wohnort: Seelow, Deutschland

Beitragvon Ignat » Sonntag 18. April 2004, 16:27

Oh..
What can I say, Marty?
Only one thing:
3:0! :)
Yes, it really what I need. :)
Thank you very much!
If the ship don't know where to go - it has no fair wind...
Ignat
Alter Hase
 
Beiträge: 80
Registriert: Samstag 2. August 2003, 11:13

Re: Get all variants?..

Beitragvon Sumer » Samstag 17. Januar 2015, 06:44

Hi,

This is exceptionally decent and wonderful post.
I am very happy joine this forum.
I like it exceptionally much....!!!!
Thanks alot... :) 8)
Sumer
Sumer
Newbie
 
Beiträge: 1
Registriert: Samstag 17. Januar 2015, 06:42


Zurück zu Java

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder

cron