Einzelnen Beitrag anzeigen
Alt 15.02.08, 14:55   #15 (permalink)
xsheep
 
Registriert seit: 29.01.06
xsheep Leistung: Facit NTK
Likes: 0
Standard

Meine Lösung ist in C geschrieben:

Code:
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <limits.h>

# define MAX 49
# define MIN 1

int doppelt(unsigned int array[], int pos){

    int i;

    for(i=0; i<pos; i++)
        if(array[pos]==array[i])
            return 1;

    return 0;

}

int zufall_ohne_zuruecklegen(unsigned int array[], int groesse, int min, int max){

    int i;

    for(i=0; i<groesse; i++){

        do{

            array[i]=min+rand()%(max-min+1);

        }while(doppelt(array, i));

    }

}

int pruefe(unsigned int array_1[], unsigned int array_2[], int groesse){

    int i, n;
    int treffer;

    for(i=0, treffer=0; i<groesse; i++)
        for(n=0; n<groesse; n++)
            if(array_1[i]==array_2[n])
                treffer++;

    return treffer;

}

int main(){

    int i;
    unsigned long zaehler;
    unsigned int tipp[6], zufall[6];

    for(i=0; i<6; i++){

        printf("%d. Zahl: ", i+1);
        scanf("%d", &tipp[i]);

        if(doppelt(tipp, i) || tipp[i]>MAX || tipp[i]<MIN){

            printf("Zahl ungueltig\n");
            i--;

        }

    }

    srand(time(NULL));
    for(zaehler=0; zaehler<=ULONG_MAX; zaehler++){

        zufall_ohne_zuruecklegen(zufall, 6, MIN, MAX);

        if(pruefe(tipp, zufall, 6)==6){

            printf("6 Treffer nach %d Versuchen, die Wahrscheinnlichkeit liegt bei %.9f%%.", zaehler, (double)1/zaehler);
            return 0;

        }

    }

}
xsheep ist offline   Mit Zitat antworten
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61