C# :: Aufgabe #226

3 Lösungen Lösungen öffentlich

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}

Lösungen:

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));
		}
	}
}
vote_ok
von MadBit (110 Punkte) - 23.11.2018 um 12:36 Uhr
Quellcode ausblenden C#-Code
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;
        }
    }
}
vote_ok
von hollst (13980 Punkte) - 22.02.2019 um 10:03 Uhr
Quellcode ausblenden C#-Code
using System;
using static System.Console;
using System.Collections.Generic;

namespace Aufgabe_226
{
    static class Program
    {
        static void Main()
        {
            uint[] AMAX = new uint[] { 3, 2, 9, 1, 4};
            List<uint[]> A = AMAX.AllNumbers();
            WriteLine($"AllNumbers_TEST(): {A.Count}");
            for(var i = 0; i < A.Count; i++)
            {
                Write($"{i,4}: ");
                for(var j = 0; j < A[i].Length; j++)
                    Write($"{A[i][j],2} ");
                WriteLine();
            }            
            ReadKey();
        }

        public static List<uint[]> AllNumbers(this uint[] AMAX)
        {
            List<uint[]> result = new List<uint[]>();
            uint maxn = 1;
            for (var i = 0; i < AMAX.Length; i++)
                maxn *= AMAX[i];

            uint[] A = new uint[AMAX.Length];
            
            for(var i = 0; i < maxn; i++)
            {
                uint[] AA = new uint[AMAX.Length];
                A.CopyTo(AA, 0);
                result.Add(AA);

                for (var j = 0; j < AMAX.Length; j++)
                    if(A[j] < AMAX[j] - 1)
                    {
                        A[j]++; break;
                    }
                    else
                        A[j] = 0;                                                
            }
            return result;
        }
    }
}