2016-04-20 8 views
1

Ich versuche, alle Kombinationen (5C1, 5C2, 5C3, 5C4, 5C5) von 1,2,3,4,5 in einzelnen Array zu halten. Also muss ich dynamisches Array mit for-Schleife in C# erstellen.Wie erstellt man eindimensionale, zweidimensionale, dreidimensionale sowie multidimensionale Arrays mit C#?

Nehmen wir zum Beispiel hier n = 5 und r = 1 bis 5. wenn r = 1, dann My Array eindimensionales Array sein wird, wenn r = 2, dann wird es die zweidimensionale Anordnung sein, wenn r = 3 dann dreidimensional, wenn r = 4 dann vierdimensionalen Array und es wird von 5. Mein Code E fortgesetzt bis Ende unterhalb

string[] ShipArrayObj; 
    public frmResult(string[] ShipArray) 
    { 
     InitializeComponent();   
     ShipArrayObj = ShipArray; 
    } 

    private void frmResult_Load(object sender, EventArgs e) 
    { 
     string[] arr = ShipArrayObj;   
     int n = ShipArrayObj.Count(); 
     for (int r = 1; r <= n; r++) 
     {     
      StoreCombination(arr, n, r); 
      richTextBox1.Text = richTextBox1.Text + "/";     
     } 

    } 

    void StoreCombination(string[] arr, int n, int r) 
    {   
     string[] data = new string[r];    
     createCombination (arr, data, 0, n - 1, 0, r); 
    } 


    private void createCombination(string[] arr, string[] data, int start, int end, int index, int r) 
    { 
     if (index == r) 
     { 
      int j = 0; 
      for (j = 0; j < r; j++) 
      richTextBox1.Text = richTextBox1.Text + data[j].ToString();//Where I want to set array to keep combination values 
      return; 
     } 

     int i = 0; 
     for (i = start; i <= end && end - i + 1 >= r - index; i++) 
     { 
      data[index] = arr[i]; 
      CreateCombination(arr, data, i + 1, end, index + 1, r); 
     } 
    } 

gegeben ich die ganze Kombination in ein Rich-Text-Box bin speichern, aber möchte in einer Reihe bleiben. Wenn mir jemand hilft, werde ich Ihnen dankbar sein.

Antwort

2

Wenn Sie an etwas wie Java gewöhnt sind, unterscheiden sich mehrdimensionale Arrays in C# etwas in der Syntax.

Here's a page describing how to do them in C#. Hier ist ein Ausschnitt aus der Seite:

// Two-dimensional array. 
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; 
// The same array with dimensions specified. 
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; 
// A similar array with string elements. 
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" }, 
             { "five", "six" } }; 

// Three-dimensional array. 
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
           { { 7, 8, 9 }, { 10, 11, 12 } } }; 
// The same array with dimensions specified. 
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
             { { 7, 8, 9 }, { 10, 11, 12 } } }; 

If you're interested in different combinations of things with a fixed number of them, something like this should be all you need.

If you're interested in different combinations of things with a dynamic number of them, something like this should be all you need.

(Es sei denn, Sie versuchen, die Leistung zu optimieren, ist es besser lesbar/ausdruck zu sein, allgemein gesprochen.)

Möglicherweise müssen Sie prüfen, ob eine Bestellung wichtig ist oder nicht (nicht bestellter Satz) vs. geordnete Liste). Ich würde annehmen, dass es nicht von Ihrem Code (in welchem ​​Fall das Sortieren gut ist, um "Duplikate" zu eliminieren), aber ich kann nicht sicher sagen.


Hier ist ein gutes Beispiel für Variationen zu lesen und ändern einfach ist, und ist nicht so schlecht für kleine Zahlen:

// -1, 0, ..., 5 
var choices = Enumerable.Range(-1, 6); 

var possibleChoices = 
    from a in choices 
    from b in choices 
    from c in choices 
    from d in choices 
    from e in choices 
    select (IEnumerable<int>)new [] { a, b, c, d, e }; 

// Remove -1's because they represent not being in the choice. 
possibleChoices = 
    possibleChoices.Select(c => c.Where(d => d >= 0)); 

// Remove choices that have non-unique digits. 
possibleChoices = 
    possibleChoices.Where(c => c.Distinct().Count() == c.Count()); 

// Sort the choices to indicate order doesn't matter 
possibleChoices = 
    possibleChoices.Select(c => c.OrderBy(d => d)); 

// Remove duplicates 
possibleChoices = 
    possibleChoices.Select(c => new 
          { 
           Key = string.Join(",", c), 
           Choice = c 
          }). 
    GroupBy(c => c.Key). 
    Select(g => g.FirstOrDefault().Choice); 

foreach (var choice in possibleChoices) { 
    Console.Out.WriteLine(string.Join(", ", choice)); 
} 

Ausgang:

0 
1 
2 
3 
4 
0, 1 
0, 2 
0, 3 
0, 4 
1, 2 
1, 3 
1, 4 
2, 3 
2, 4 
3, 4 
0, 1, 2 
0, 1, 3 
0, 1, 4 
0, 2, 3 
0, 2, 4 
0, 3, 4 
1, 2, 3 
1, 2, 4 
1, 3, 4 
2, 3, 4 
0, 1, 2, 3 
0, 1, 2, 4 
0, 1, 3, 4 
0, 2, 3, 4 
1, 2, 3, 4 
0, 1, 2, 3, 4 

Dies ist wahrscheinlich ein wenig dichter zu verstehen, fest auf diese spezifische Variation der Kombination codiert und beinhaltet Rekursion, ist aber ein bisschen mehr generi c/ist nicht fest codiert auf 5 (und nahm 0.047s auf dotnetfiddle.net statt 0.094s). Es ist auch völlig faul/IEnumerable.

public static void Main() 
{  
    var possibleChoices = Choose(5); 

    foreach (var choice in possibleChoices) { 
     Console.Out.WriteLine(string.Join(", ", choice)); 
    } 
} 

public static IEnumerable<IEnumerable<int>> Choose(int max) 
{  
    var remaining = Enumerable.Range(0, max); 

    return ChooseRecursive(remaining, Enumerable.Empty<int>()); 
} 

public static IEnumerable<IEnumerable<int>> ChooseRecursive(IEnumerable<int> remaining, IEnumerable<int> chosen) 
{  
    yield return chosen; 

    foreach (var digit in remaining) 
    { 
     var choices = ChooseRecursive(
      remaining.Where(d => d > digit), 
      chosen.Concat(new [] { digit }) 
     ); 
     foreach (var choice in choices) 
     {     
      yield return choice; 
     } 
    } 
} 

Ausgang:

0 
0, 1 
0, 1, 2 
0, 1, 2, 3 
0, 1, 2, 3, 4 
0, 1, 2, 4 
0, 1, 3 
0, 1, 3, 4 
0, 1, 4 
0, 2 
0, 2, 3 
0, 2, 3, 4 
0, 2, 4 
0, 3 
0, 3, 4 
0, 4 
1 
1, 2 
1, 2, 3 
1, 2, 3, 4 
1, 2, 4 
1, 3 
1, 3, 4 
1, 4 
2 
2, 3 
2, 3, 4 
2, 4 
3 
3, 4 
4 
+0

Ihre gegebene Ausgabe ist OK. Aus dieser Liste möchte ich eine zufällige Reihe von Werten auswählen, indem ich eine Schleife oder andere Möglichkeiten verwende, so dass ich diese Werte in einer Operation verwenden kann. Wenn Sie bitte einen Weg geben können, Wie kann ich diesen Wert in einem Array oder einer Liste halten? @WLJ –

+0

@PritamJyotiRay Rufen Sie ToArray() im IEnumerable-Objekt auf und wählen Sie dann eines in einem zufälligen Index aus. Wenn Sie dies mehr als einmal tun müssen, speichern Sie den ToArray-Aufruf für jede eindeutige Ganzzahl zwischen. Wenn Sie durchgehen möchten, erstellen Sie eine Datenstruktur, die mischt und dann jedes Element sukzessive zurückgibt, bis es jedes durchläuft. In diesem Fall wird erneut gemischt. Stellen Sie eine neue Frage, wenn Sie eine Implementierung wünschen :) –

Verwandte Themen