C# :: Aufgabe #227
6 Lösungen
Durchnummerierung aller möglichen geordneten Ziehungsergebnisse bei 6 aus 49
Anfänger - C#
von hollst
- 06.09.2018 um 10:28 Uhr
Beim Lotto 6 aus 49 gibt es bekanntlich 13.983.816 mögliche (geordnete) Ziehungsergebnisse (49 über 6).
Schreibe ein Programm, das jeder dieser Möglichkeiten eine natürliche Zahl zuordnet, also
1 2 3 4 5 6 -> 1
1 2 3 4 5 7 -> 2
...
...
44 45 46 47 48 49 -> 13.983.816
Viel Spaß!
Schreibe ein Programm, das jeder dieser Möglichkeiten eine natürliche Zahl zuordnet, also
1 2 3 4 5 6 -> 1
1 2 3 4 5 7 -> 2
...
...
44 45 46 47 48 49 -> 13.983.816
Viel Spaß!
Lösungen:
Ich habe eine Lösung. Aber das geht bestimmt noch besser. Habe nach 2 Minuten ca 0.01 % gehabt xD.
C#-Code
using System;
namespace LottoNummerierung
{
class Program
{
static int MAX = 49;
static int N = 6;
static void Main(string[] args)
{
int[] arr = new int[N];
string[] sol = new string[13983816];
for (int i = 0; i < arr.Length; i++)// Erstes befüllen
{
arr[i] = i + 1;
}
sol[0] = BuildSoloution(arr, 1);
EvaluateValue(ref arr,ref sol);
PrintSoloution(sol);
Console.ReadKey();
}
static void PrintSoloution(string[] sol)
{
foreach (string s in sol)
{
if (s != null && s.Length > 0)
Console.WriteLine(s);
}
}
static void EvaluateValue(ref int[] arr, ref string[] sol)
{
int pos = arr.Length - 1;
int c = 1;
while (pos >= 0)
{
if (!AddOneToArray(ref arr, pos))
{
for (int j = pos; j < arr.Length; j++)
{
arr[j] = GetMinValue(arr);
}
pos--;
}
else
{
if (pos + 1 < arr.Length)
pos++;
sol[c] = BuildSoloution(arr, c + 1);
}
}
}
static bool AddOneToArray(ref int[] arr, int pos)
{
if (arr[pos] < MAX)
{
int c = 1;
while (!CheckIfNumberIsFree(ref arr, arr[pos] + c))
{
c++;
}
if (arr[pos] + c <= MAX)
{
arr[pos] += c;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
static int GetMinValue(int[] arr)
{
int ret = 1;
while (ret != MAX)
{
bool isIn = false;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == ret)
{
isIn = true;
break;
}
}
if (isIn)
ret++;
else
return ret;
}
return ret;
}
static bool CheckIfNumberIsFree(ref int[] arr, int number)
{
bool ret = true;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == number)
{
ret = false;
break;
}
}
return ret;
}
static string BuildSoloution(int[] arr, int pos)
{
string s = "";
for (int i = 0; i < arr.Length; i++)
{
s += " " + arr[i];
}
return pos + " ->" + s;
}
}
}
using System;
namespace ConsoleLottoNummerierung
{
class Program
{
static int i = 1;
static int[] list = new int[6];
static void Main(string[] args)
{
Generate();
}
static void Generate(int from = 1, int to = 44, int depth = 6)
{
if (depth == 0)
{
Console.WriteLine($"{list[0],2}, {list[1],2}, {list[2],2}, {list[3],2}, {list[4],2}, {list[5],2} - {i++,8}");
return;
}
for (int i = from; i <= to; i++)
{
list[6 - depth] = i;
Generate(i + 1, to + 1, depth - 1);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lottozahlen
{
class Program
{
static void Main(string[] args)
{
int zahl = 0;
int zahl1 = 1;
int zahl2 = 2;
int zahl3 = 3;
int zahl4 = 4;
int zahl5 = 5;
int zahl6 = 6;
int zahlEnde = 1;
for (zahl1 = 1; zahl1 < 49; zahl1++)
{
zahl = zahl1;
while ( zahl == zahl2 | zahl == zahl3 | zahl == zahl4 | zahl == zahl5 | zahl == zahl6)
{
zahl1++;
zahl = zahl1;
}
Zahl6(zahl1, zahl2, zahl3, zahl4, zahl5, ref zahl6, ref zahlEnde, zahl);
zahl6 = zahl1 + 1;
Zahl5(zahl1, zahl2, zahl3, zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
zahl5 = zahl1 + 1;
Zahl4(zahl1, zahl2, zahl3, ref zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
zahl4 = zahl1 + 1;
Zahl3(zahl1, zahl2, ref zahl3, ref zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
zahl3 = zahl1 + 1;
Zahl2(zahl1, ref zahl2, ref zahl3, ref zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
zahl2 = zahl1 + 1;
Ausgabe(zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahlEnde, zahl);
zahlEnde++;
}
Console.ReadKey();
}
private static void Zahl2(int zahl1, ref int zahl2, ref int zahl3, ref int zahl4, ref int zahl5, ref int zahl6, ref int zahlEnde, int zahl)
{
for (zahl2 = zahl1 + 1; zahl2 < 49; zahl2++)
{
zahl = zahl2;
while (zahl == zahl1 | zahl == zahl3 | zahl == zahl4 | zahl == zahl5 | zahl == zahl6)
{
zahl2++;
zahl = zahl2;
}
Ausgabe(zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahlEnde, zahl);
zahlEnde++;
Zahl6(zahl1, zahl2, zahl3, zahl4, zahl5, ref zahl6, ref zahlEnde, zahl);
Zahl5(zahl1, zahl2, zahl3, zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
Zahl4(zahl1, zahl2, zahl3, ref zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
Zahl3(zahl1, zahl2, ref zahl3, ref zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
}
}
private static void Zahl3(int zahl1, int zahl2, ref int zahl3, ref int zahl4, ref int zahl5, ref int zahl6, ref int zahlEnde, int zahl)
{
for (zahl3 = zahl1 + 1; zahl3 < 49; zahl3++)
{
zahl = zahl3;
while (zahl == zahl1 | zahl == zahl2 | zahl == zahl4 | zahl == zahl5 | zahl == zahl6)
{
zahl3++;
zahl = zahl3;
}
Ausgabe(zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahlEnde, zahl);
zahlEnde++;
Zahl6(zahl1, zahl2, zahl3, zahl4, zahl5, ref zahl6, ref zahlEnde, zahl);
Zahl5(zahl1, zahl2, zahl3, zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
Zahl4(zahl1, zahl2, zahl3, ref zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
}
}
private static void Zahl4(int zahl1, int zahl2, int zahl3, ref int zahl4, ref int zahl5, ref int zahl6, ref int zahlEnde, int zahl)
{
for (zahl4 = zahl1 + 1; zahl4 < 49; zahl4++)
{
zahl = zahl4;
while (zahl == zahl1 | zahl == zahl2 | zahl == zahl3 | zahl == zahl5 | zahl == zahl6)
{
zahl4++;
zahl = zahl4;
}
Ausgabe(zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahlEnde, zahl);
zahlEnde++;
Zahl6(zahl1, zahl2, zahl3, zahl4, zahl5, ref zahl6, ref zahlEnde, zahl);
Zahl5(zahl1, zahl2, zahl3, zahl4, ref zahl5, ref zahl6, ref zahlEnde, zahl);
}
}
private static void Zahl5(int zahl1, int zahl2, int zahl3, int zahl4, ref int zahl5, ref int zahl6, ref int zahlEnde, int zahl)
{
for (zahl5 = zahl1 + 1; zahl5 < 49; zahl5++)
{
zahl = zahl5;
while (zahl == zahl1 | zahl == zahl2 | zahl == zahl3 | zahl == zahl4 | zahl == zahl6)
{
zahl5++;
zahl = zahl5;
}
Ausgabe(zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahlEnde, zahl);
zahlEnde++;
Zahl6(zahl1, zahl2, zahl3, zahl4, zahl5, ref zahl6, ref zahlEnde, zahl);
}
}
private static void Zahl6(int zahl1, int zahl2, int zahl3, int zahl4, int zahl5, ref int zahl6, ref int zahlEnde, int zahl)
{
for (zahl6 = zahl1 + 1; zahl6 < 49; zahl6++)
{
zahl = zahl6;
while (zahl == zahl1 | zahl == zahl2 | zahl == zahl3 | zahl == zahl4 | zahl == zahl5 )
{
zahl6++;
zahl = zahl6;
}
Ausgabe(zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahlEnde, zahl);
zahlEnde++;
}
}
private static void Ausgabe(int zahl1, int zahl2, int zahl3, int zahl4, int zahl5, int zahl6, int zahlEnde, int zahl)
{
if (zahl1 < 50 & zahl2 < 50 & zahl3 < 50 & zahl4 < 50 & zahl5 < 50 & zahl6 < 50)
{
Console.WriteLine("{0} - {1} - {2} - {3} - {4} - {5} : {6}", zahl1, zahl2, zahl3, zahl4, zahl5, zahl6, zahlEnde);
}
}
}
}
using System;
using static System.Console;
using System.Collections.Generic;
using System.Text;
namespace aufgabe_227__6_aus_49
{
class Program
{
static void Main()
{
string NL = Environment.NewLine;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
List<byte[]> alle_moeglichen_ziehungen = tools.Enum_NueberM(49, 6);
sw.Stop();
WriteLine("rechenzeit: " + sw.Elapsed.Dauer() + NL);
WriteLine(alle_moeglichen_ziehungen[0].ToMyString());
WriteLine(alle_moeglichen_ziehungen[1].ToMyString());
WriteLine(NL + "------------------" + NL);
WriteLine(alle_moeglichen_ziehungen[alle_moeglichen_ziehungen.Count - 1 - 1].ToMyString());
WriteLine(alle_moeglichen_ziehungen[alle_moeglichen_ziehungen.Count - 1 - 0].ToMyString());
ReadKey();
}
}
public static class tools
{
public static System.Numerics.BigInteger NueberM(int n, int m)
{
System.Numerics.BigInteger result = 1;
for (var i = 0; i < m; i++)
result *= n--;
for (var i = 1; i <= m; i++)
result /= i;
return result;
}
public static List<byte[]> Enum_NueberM(int n, int m)// 6 aus 49 -> (49, 6)
{
List<byte[]> result = new List<byte[]>();
System.Numerics.BigInteger max_loop = NueberM(n, m);
byte[] b = new byte[m], bmax = new byte[m];
for (var i = 0; i < b.Length; i++)
{
b[i] = (byte)(i + 1);
bmax[bmax.Length - 1 - i] = (byte)(n - i);
}
for (var i = 0; i < max_loop - 1; i++)
{
byte[] bb = new byte[b.Length];
b.CopyTo(bb, 0);
result.Add(bb);
int counter = 0;
for (var j = b.Length - 1; j >= 0; j--)
if (b[j] < bmax[j])
break;
else
counter++;
b[b.Length - 1 - counter]++;
for (var j = 1; j <= counter; j++)
b[b.Length - 1 - counter + j] = (byte)(b[b.Length - 1 - counter] + j);
}
result.Add(b);
return result;
}
//----------------------------------------------------------------------------------
public static string ToMyString(this byte[] b)
{
StringBuilder sb = new StringBuilder();
for (var i = 0; i < b.Length; i++)
sb.Append($"{b[i],3}");
return sb.ToString();
}
public static string Dauer(this TimeSpan ts) =>
String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lotto_6_aus_49
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Willkommen zum Lottospiel 6 aus 49!");
Console.WriteLine("Um alle Möglichen Kombinationen zu sehen, drücken Sie einfach Enter!");
Console.ReadKey();
int z = 0;
Console.WriteLine("Kombinationen: ");
for (int z1 = 1; z1 < 45; z1++)
{
for (int z2 = z1 +1; z2 < 46; z2++)
{
for (int z3 = z2 +1; z3 < 47; z3++)
{
for (int z4 = z3 + 1; z4 < 48; z4++)
{
for (int z5 = z4 + 1; z5 < 49; z5++)
{
for (int z6 = z5 + 1; z6 < 50; z6++)
{
z++;
Console.WriteLine(" " + z1 + "," + z2 + "," + z3 + "," + z4 + "," + z5 + "," + z6 + "->" + z);
}
}
}
}
}
}
Console.ReadKey();
}
}
}
{
int counter = 0;
for (int zahl1 = 1; zahl1 <= 44; zahl1++)
{
for (int zahl2 = (zahl1 + 1); zahl2 <= 45; zahl2++)
{
for (int zahl3 = (zahl2 + 1); zahl3 <=46 ; zahl3++)
{
for (int zahl4 = (zahl3 + 1); zahl4 <=47; zahl4++)
{
for (int zahl5 = (zahl4 + 1); zahl5 <= 48; zahl5++)
{
for (int zahl6 = (zahl4+1); zahl6 <=49 ; zahl6++)
{
counter++;
Console.WriteLine($"{zahl1} {zahl2} {zahl3} {zahl4} {zahl5} {zahl6} -> \t{counter}");
}
}
}
}
}
}
} 