C# :: Aufgabe #226 :: Lösung #2
3 Lösungen

#226
Digitbegrenztes Zählen
Anfänger - C#
von hollst
- 31.08.2018 um 11:04 Uhr
Gegeben sei ein positives Integerarray (uint) AMAX der Länge N, das mit Zufallszahlen zwischen (inclusive) 1 und einer Konstanten amax gefüllt ist.
Die Feldbelegung von AMAX stellt obere Wertgrenzen dar.
Man schreibe ein Programm, das alle möglichen Belegungen eines positives Integerarrays A (Länge N) auflistet,
wobei die Feldwerte von A entsprechend der Grenzwertbelegung von AMAX stets unterschritten werden müssen.
Beispiel (c#):
C#-Code
Die Feldbelegung von AMAX stellt obere Wertgrenzen dar.
Man schreibe ein Programm, das alle möglichen Belegungen eines positives Integerarrays A (Länge N) auflistet,
wobei die Feldwerte von A entsprechend der Grenzwertbelegung von AMAX stets unterschritten werden müssen.
Beispiel (c#):

int N = 5, amax = 11; uint[] AMAX = new uint[] {3, 2, 9, 1, 4} //mögliche uint[] A sind: new uint[] {0, 0, 0, 0, 0} new uint[] {1, 0, 0, 0, 0} new uint[] {2, 0, 0, 0, 0} new uint[] {0, 1, 0, 0, 0} new uint[] {1, 1, 0, 0, 0} new uint[] {2, 1, 0, 0, 0} new uint[] {0, 0, 1, 0, 0} // ... // ... new uint[] {2, 1, 8, 0, 3}
#2

von MadBit (110 Punkte)
- 23.11.2018 um 12:36 Uhr

using System; using System.Collections.Generic; using System.Linq; namespace ArrayCounter { class Program { const uint N = 5; const uint amax = 11; static uint[] AMAX; static List<uint[]> A = new List<uint[]>(); static void Main(string[] args) { Vorbereiten(); ArrayFuellen(); PrintArray(); } static void Vorbereiten() { Random rnd = new Random(); AMAX = new uint[N]; for (uint i = 0; i < N; i++) AMAX[i] = (uint)rnd.Next(1, (int)amax); } static void ArrayFuellen() { A.Clear(); uint[] counter = new uint[N]; while(true) { uint[] a = new uint[N]; for (uint i = 0; i < N; i++) { if (counter[i] >= AMAX[i]) { if (i < (N - 1)) { counter[i] = 0; counter[i + 1]++; } } a[i] = counter[i]; } if (counter[N - 1] == AMAX[N - 1]) break; counter[0]++; A.Add(a); } } static void PrintArray() { foreach (uint[] item in A) { Console.WriteLine(IntegerArrayZuString(item)); } Console.WriteLine("Grenzwerte"); Console.WriteLine(IntegerArrayZuString(AMAX)); } static string IntegerArrayZuString(uint[] ar) { string ausgabe = string.Empty; for (int i = 0; i < ar.Count(); i++) { ausgabe += ar[i].ToString(); if (i < (ar.Count() - 1)) ausgabe += ", "; } return ausgabe; } } }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1