C# :: Aufgabe #191

1 Lösung Lösung öffentlich

Anzahl möglicher Objektkombinationen

Fortgeschrittener - C# von mty - 16.10.2017 um 19:13 Uhr
Ich möchte die Anzahl aller möglicher Objektkombinationen Z haben, die zu einer bestimmten Anzahl N möglich sind.

Eingabe:

ARRAY Z : object a, object b, object c, object d ... etc
INTEGER N: 10

Ausgabe:
a a a a a a a a a a
a a a a a a a a a b
a a a a a a a a b b
a a a a a a a b b b
a a a a a a b b b b
a a a a a b b b b b
a a a a b b b b b b
a a a b b b b b b b
a a b b b b b b b b
a b b b b b b b b b
b b b b b b b b b b
b b b b b b b b c c
b b b b b b b c c c
b b b b b b c c c c
b b b b b c c c c c
b b b b c c c c c c
....... usw.

Die Anzahl der Objektkombinationen Z kann nicht größer sein als die Anzahl N. Das ganze soll vom Benutzer eingestellt werden können.
Zusätzlich soll das Ergebnis in einem Array gespeichert werden und dann eine eindeutige Eigenschaft jedes Objektes ausgegeben werden können.

Wer will kann sich gerne Versuchen.




Lösungen:

vote_ok
von hollst (13980 Punkte) - 23.10.2017 um 16:10 Uhr
Quellcode ausblenden C#-Code
using System;
using static System.Console;

using System.Numerics;

namespace aufgabe_191
{
    class Program
    {
        static string NL = Environment.NewLine;
        static void Main()
        {
            bool bo_again = true;
            while (bo_again)    {

                WriteLine();
                int z = 0;                  //Anzahl unterschiedlicher Objekte (hier max 99, sonst wird es zu unübersichtlich,
                                            //ist aber prinzipiell unbeschränkt)
                bool bo_input_ok = false;
                while (!bo_input_ok)    {
                    string input = "give me count of objects (1 ... 99): ".ReadValue();
                    bo_input_ok = int.TryParse(input, out z);
                    if ((z < 1) || (z > 99))
                        bo_input_ok = false;
                }

                BigInteger bmax = BigInteger.Pow((BigInteger)z, z);
                ("max possible object arrangements: " + bmax.ToString("E1") + "  exact: " + bmax.ToString("n0")).MessageLine();

                BigInteger bvalue = 0;
                bo_input_ok = false;
                while (!bo_input_ok)    {
                    string input = ("take an example (0 ... " + (bmax - 1).ToString("E1") + "): ").ReadValue();
                    bo_input_ok = BigInteger.TryParse(input, out bvalue);
                    if (bvalue > bmax - 1)
                        bo_input_ok = false;
                }

                (z.value_for_this_basis(bvalue).BIArrayToString() + NL + "= " +
                    z.value_for_this_basis(bvalue).cut_leading_zeros().BIArrayToString() + " (BASE " + z.ToString() + ")").MessageLine();
                bo_again = !("press key: again (all except ESC) or EXIT (ESC)".Readkey().Key == ConsoleKey.Escape);
            }
        }
    }

    static class MyExtensions
    {
        public static void MessageLine(this string info) => WriteLine(info);

        public static string ReadValue(this string intro)        {
            Write(intro); return ReadLine();
        }

        public static ConsoleKeyInfo Readkey(this string intro)        {
            Write(intro);
            ConsoleKeyInfo ki = ReadKey(true);
            return ki;
        }

        //__ Hauptmethode
        public static BigInteger[] value_for_this_basis(this int basis, BigInteger dec_value)
        {
            BigInteger[] result = new BigInteger[basis];
            int array_pos = result.Length - 1;

            for(int i = 0; i < result.Length; i++)  {
                BigInteger rest = dec_value % basis;
                result[array_pos--] = rest;                
                dec_value /= basis;
            }
            return result;
        }
        //__

        public static BigInteger[] cut_leading_zeros(this BigInteger[] value)
        {
            System.Collections.Generic.List<BigInteger> L = new System.Collections.Generic.List<BigInteger>();
            bool bo_found_first_not_zero = false;
            for(var i = 0; i < value.Length; i++)
            {
                if(value[i] != 0)
                    bo_found_first_not_zero = true;
                if (bo_found_first_not_zero)
                    L.Add(value[i]);
            }
            return L.ToArray();
        }

        public static string BIArrayToString(this BigInteger[] bia)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (var i = 0; i < bia.Length; i++)
                sb.Append(bia[i].ToString("n0") + " ");
            return sb.ToString();
        }
    }
}
1810266

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.