Entwickler-Ecke
Basistechnologien - Mögliche Kombinationen
TheSoul - Di 20.10.09 09:35
Titel: Mögliche Kombinationen
Morgen Allerseits
Bin wieder am C-Sharp üben und habe die Aufgabe bekommen eine Konsolen-Applikation zu machen. Diese Applikation sollte die alle möglichen Kombinationen des Inputs aufzählen.
Beispiel :
Input: ABC
Output:
ABC
CBA
BCA
CAB
ACB
BAC
Ich möchte nicht von euch, dass ihr die Aufgabe löst, sondern mir zum weiterdenken helft. Ich habe keine Ahnung wie man diese Sache angeht.
Hier mal mein Code bis hier, weiter hab ichs nicht gebracht.
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
| using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace vertauscher { class Program { public string vertauscher(string input, int position1, int position2) { if (position1 != position2) { StringBuilder tmp = new StringBuilder(input); tmp.Remove(position1, 1); tmp.Insert(position1, input[position2]); tmp.Remove(position2, 1); tmp.Insert(position2, input[position1]); return tmp.ToString(); }
return input; } static void Main(string[] args) { string a = "ABCD";
} } } |
TheSoul - Di 20.10.09 11:01
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
| using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace vertauscher { class Program { static int zaehler = 1; static void Main(string[] args) { string input = "ABCDEFGH"; permutation(input, 0); Console.In.ReadLine(); }
public static void permutation(string input, int position) { if (position == input.Length - 1) { Console.Out.WriteLine(zaehler++ + ": " + input); return; }
for (int i = position; i < input.Length; i++) { input = vertausche(input, position, i); permutation(input, position + 1); input = vertausche(input, position, 1); }
} public static string vertausche(string input, int position1, int position2) { if (position1 != position2) { StringBuilder tmp = new StringBuilder(input); tmp.Remove(position1, 1); tmp.Insert(position1, input[position2]); tmp.Remove(position2, 1); tmp.Insert(position2, input[position1]); return tmp.ToString(); }
return input; }
} } |
Nemag - Di 20.10.09 14:47
Hab nur mal ne kurze Zwischenfrage bevor ich was richtiges zu posten hab.
Wie soll denn mit doppelter Eingabe verfahren werden? Soll also wirklich alle Kombinationen ausgegeben werden oder Unique(Alle Kombination).
Eingabe: AAB
AAB, ABA, BAA
oder
AAB,AAB, ABA,ABA, BAA, BAA ?
TheSoul - Di 20.10.09 15:01
Bei Eingabe von AAB gibt es folgendes aus:
1: AAB
2: ABA
3: AAB
4: ABA
5: BAA
6: BAA
Freundliche Grüsse
TheSoul
TheSoul - Di 20.10.09 15:03
Danke für deine Hilfe nemag. Jedoch bin ich mit dieser Aufgabe schon fertig geworden (Siehe zweiter Beitrag). Ich werde mir trotzdem deinen Link anschauen und die Lösung mit meiner Vergleichen.
Entwickler-Ecke.de based on phpBB
Copyright 2002 - 2011 by Tino Teuber, Copyright 2011 - 2025 by Christian Stelzmann Alle Rechte vorbehalten.
Alle Beiträge stammen von dritten Personen und dürfen geltendes Recht nicht verletzen.
Entwickler-Ecke und die zugehörigen Webseiten distanzieren sich ausdrücklich von Fremdinhalten jeglicher Art!