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


Zurück zu Java


Social Bookmarking
Bookmark bei: Mr. Wong Bookmark bei: Del.icio.us Bookmark bei: Webnews Bookmark bei: Icio Bookmark bei: Oneview Bookmark bei: Linkarena Bookmark bei: Newskick Bookmark bei: Folkd Bookmark bei: Yigg Bookmark bei: Digg Bookmark bei: Reddit Bookmark bei: Simpy Bookmark bei: StumbleUpon Bookmark bei: Slashdot Bookmark bei: Netscape Bookmark bei: Furl Bookmark bei: Yahoo Bookmark bei: Spurl Bookmark bei: Google Bookmark bei: Blinklist Bookmark bei: Blogmarks Bookmark bei: Diigo Bookmark bei: Technorati Bookmark bei: Newsvine Bookmark bei: Blinkbits Bookmark bei: Ma.Gnolia Bookmark bei: Smarking Bookmark bei: Netvouz

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder

cron