C# :: Aufgabe #226 :: Lösung #1

3 Lösungen Lösungen öffentlich
#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#):
Quellcode ausblenden C#-Code
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}
#1
vote_ok
von Z3RP (1020 Punkte) - 04.09.2018 um 08:57 Uhr
Ok hatte es doch noch verstanden :)
Du machst übrigens echt gute Aufgaben. Bei manchen kommt sich vor wie in der Schule aber manche sind auch echt schwierig.

Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;

namespace Digitbegrenztes_Zählen
{
	class Program
	{
		static uint[] AMAX;
		static readonly Random Rnd = new Random();

		static void Main(string[] args)
		{
			int N = 5;
			int amax = 11;
			AMAX = new uint[N];
			for (int i = 0; i < AMAX.Length; i++)
			{
				AMAX[i] = (uint)Rnd.Next(amax)+1;
			}


			foreach (uint[] arr in GenerateArrays())
			{
				ShowUintArray(arr);
			}

			Console.WriteLine("============================");
			ShowUintArray(AMAX);

			Console.ReadKey();
		}

		static List<uint[]> GenerateArrays()
		{
			List<uint[]> retArrays = new List<uint[]>();
			uint[] lastArray = new uint[AMAX.Length];
			uint[] currentArray = new uint[AMAX.Length];

			for (int i = 0; i < AMAX.Length; i++)
			{
				lastArray[i] = AMAX[i] - 1;
				currentArray[i] = 0;
			}
			retArrays.Add(currentArray);

			while (!CompareArray(currentArray, lastArray))
			{
				currentArray = AddOneToArray(currentArray);
				retArrays.Add(currentArray);
			}

			return retArrays;
		}

		static uint[] AddOneToArray(uint[] arr)
		{
			uint[] ret = new uint[arr.Length];
			arr.CopyTo(ret, 0);

			Add(0, ref ret);
			ShowUintArray(ret);
			return ret;
		}

		static bool Add(int pos, ref uint[] arr)
		{
			if (arr[pos] + 1 < AMAX[pos])
			{
				arr[pos] += 1;
				return true;
			}
			else
			{
				if (pos + 1 < arr.Length)
				{
					bool b = Add(pos + 1, ref arr);
					if (b)
						arr[pos] = 0;
					return b;
				}
				else
				{
					return false;
				}
			}
		}

		static bool CompareArray(uint[] arr1, uint[] arr2)
		{
			bool ret = true;
			for (int i = 0; i < arr1.Length; i++)
			{
				ret = arr1[i] == arr2[i];

				if (!ret)
					return ret;
			}
			return ret;
		}

		static void ShowUintArray(uint[] arr)
		{
			Console.WriteLine("[{0}]", string.Join(", ", arr));
		}
	}
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben