C :: Aufgabe #197

2 Lösungen Lösungen öffentlich

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ß!

Lösungen:

vote_ok
von 0 (0 Punkte) - 01.10.2018 um 16:06 Uhr
Quellcode ausblenden C#-Code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Lotto_6_aus_49
{
    class Program
    {

        static void Main(string[] args)
        {

            int z = 0;
            int z1 = 0;
            int z2 = z1 + 1;
            int z3 = z2 + 1;
            int z4 = z3 + 1;
            int z5 = z4 + 1;
            int z6 = z5 + 1;

            StreamWriter tw = new StreamWriter("C:\\Users\\Koerner\\Desktop\\Lotto (Modul 1)\\Lotto 6 aus 49 funktioniert While-Schleife - Nur in Datei Ausgeben\\Möglichkeiten.txt");

            Console.WriteLine("Bitte Warten, das Programm beendet sich von alleine! ");
             
            while (z1 < 44)
            {
                z1++;

                z2 = z1;
                while (z2 < 45)
                {
                    z2++;

                    z3 = z2;
                    while (z3 < 46)
                    {
                        z3++;

                        z4 = z3;
                        while (z4 < 47)
                        {
                            z4++;

                            z5 = z4;
                            while (z5 < 48)
                            {
                                z5++;

                                z6 = z5;
                                while (z6 < 49)
                                {
                                    z++;
                                    z6++;

                                    tw.WriteLine(" " + z1 + "," + z2 + "," + z3 + "," + z4 + "," + z5 + "," + z6 + "->" + z);
                                    
                                }
                            }
                        }
                    }
                }
            }
            tw.Close();
        }
    }
}

vote_ok
von manu (50 Punkte) - 31.05.2019 um 23:20 Uhr
Quellcode ausblenden C-Code
#include<stdio.h>
#include<stdlib.h>

int A=49; //Maximale Zahl im Wurf (49)

int f_dopZahl(int a, int b, int c, int d, int e, int f);
int f_ausgabe(int a, int b, int c, int d, int e, int f);


int main()
	{
	int ziehung[6];
	int x0, x1, x2, x3, x4, x5, x6;
	
	//Startaufstellung der Ziehung 1,1,1,1,1,1
	for (x0=0; x0<6; x0++)
		ziehung[x0] = 1;
	
	
	//Die erste Zahl läuft hoch	
	for(x0=0; x0<A; x0++)
		{
		//Die zweite Zahl läuft hoch	
		for(x1=0; x1<A; x1++)
			{
			//Die dritte Zahl läuft hoch	
			for(x2=0; x2<A; x2++)
				{
				//Die vierte Zahl läuft hoch	
				for(x3=0; x3<A; x3++)
					{
					//Die fünfte Zahl läuft hoch.
					for(x4=0; x4<A; x4++)			
						{
						//Die sechste Zahl läuft hoch.
						for(x5=0; x5<A; x5++)
							{
							//Ausgabe der gesamten Ziehung
							f_ausgabe(ziehung[0], ziehung[1], ziehung[2], ziehung[3], ziehung[4], ziehung[5]);				
							ziehung[5]++;
							}
						ziehung[5]=1;
						ziehung[4]++;	
						}
					ziehung[4]=1;		
					ziehung[3]++;
					}
				ziehung[3]=1;
				ziehung[2]++;
				}
			ziehung[2]=1;	
			ziehung[1]++;	
			}
		ziehung[1]=1;	
		ziehung[0]++;	
		}
	ziehung[0]=1;
	}



//Ausgabe der Ziehung
int f_ausgabe(int a, int b, int c, int d, int e, int f)
	{
	int ziehung[6];
	ziehung[0]=a;
	ziehung[1]=b;
	ziehung[2]=c;
	ziehung[3]=d;
	ziehung[4]=e;
	ziehung[5]=f;
	int y;
	int doppelt=0;
	int nummer;
	
	//Überprüfen ob die Ziehung keine doppelten Zahlen hat.
	doppelt=f_dopZahl(ziehung[0], ziehung[1], ziehung[2], ziehung[3], ziehung[4], ziehung[5]);
	
	//Ausgabe wenn keine Zahl doppelt vorhanden ist.
	if (doppelt==0)
		{
		for (y=0; y<6; y++)
			{
			printf("%i ", ziehung[y]);						
			}
		//Ausgabe der Nummer.
		if (ziehung[0]==1&&ziehung[1]==1&&ziehung[2]==1&&ziehung[3]==1&& ziehung[4]==1&&ziehung[5]==1)
			nummer=0;
		nummer++;
		printf("\t-> %i",nummer);
		printf("\n"); 
		}
	return 0;
	}
				

//Funktion zum Ausschluss doppelter Zahlen. 	
int f_dopZahl(int a, int b, int c, int d, int e, int f)
	{
	int ziehung[6];
	ziehung[0]=a;
	ziehung[1]=b;
	ziehung[2]=c;
	ziehung[3]=d;
	ziehung[4]=e;
	ziehung[5]=f;
	int x;
	
	//Vergleich Ziehung 1 auf doppeltes vorkommen.
	for (x=1; x<6; x++)
		{
		if (ziehung[0]==ziehung[x])
		return 1;	
		}
	
	//Vergleich Ziehung 2 auf doppeltes vorkommen.	
	for (x=2; x<6; x++)
		{
		if (ziehung[1]==ziehung[x])
		return 1;	
		}
	
	//Vergleich Ziehung 3 auf doppeltes vorkommen.	
	for (x=3; x<6; x++)
		{
		if (ziehung[2]==ziehung[x])
		return 1;	
		}
		
	//Vergleich Ziehung 4 auf doppeltes vorkommen.	
	for (x=4; x<6; x++)
		{
		if (ziehung[3]==ziehung[x])
		return 1;	
		}
		
	//Vergleich Ziehung 5 auf doppeltes vorkommen.	
	for (x=5; x<6; x++)
		{
		if (ziehung[4]==ziehung[x])
		return 1;	
		}
		
	return 0;		
	}