Новичок, создающий программу лото без кинов, не может понять, как сравнить все числа - PullRequest
0 голосов
/ 16 ноября 2018

Вот мои инструкции по назначению:

Вы должны написать программу, которая будет выполнять лото.

Лото состоит из 5 чисел от 1 до 70 и силового шара от цифры 1-30.

Первые 5 номеров не должны повторяться (то же самое для выигрышных номеров). Силовой шар может повторяться с любым из первых 5 чисел.

Вы собираетесь приобрести 10000 лотерейных билетов. Каждый билет имеет 6 цифры (5 цифр и 1 пау).

Дайте каждому билету случайные числа и сравните с выигрышными номерами (выигрышные номера генерируются только один раз).

Сопоставьте 5 чисел и номер Power Ball, и вы выиграете джекпот!

Совпадение только 5 номеров, и вы выиграете $ 1 000 000.

Подберите только 4 номера, и вы выиграете $ 50 000.

Совпадение только с 3 номерами, и вы выигрываете $ 7.

Матч под 3 числами, но вы получили силовой шар и выиграли $ 4.

все остальное ничего не выигрывает.

Вот мой код:

#include<iostream>
#include<time.h>
using namespace std;
int main()
{
    srand(time(0));
    int nums[6];
    int powerball;
    nums[5] = powerball;
    int win5p;
    int win5;
    int win4;
    int win3;
    int winu3p;

    for (int x = 0; x <= 10; x++)
    {
        cout << "The generated numbers are:" << endl;
        for (int x = 0; x <= 5; x++)
        {
            nums[x] = rand() % 71 + 1;
            cout << nums[x] << endl;
        }
        for (int x = 0; x < 1; x++)
        {
            cout << "The generated powerball is:" << endl;
            powerball = rand() % 31 + 1;
            cout << powerball << endl;
        }
    }

    int compnums[6];
    int comppowerball;
    compnums[5] = comppowerball;

    for (int x = 0; x <= 10; x++)
    {
        cout << "The winning numbers are:" << endl;
        for (int x = 0; x <= 5; x++)
        {
            compnums[x] = rand() % 71 + 1;
            cout << compnums[x] << endl;
        }

        cout << "The winning powerball is:" << endl;
        for (int x = 0; x < 1; x++)
        {
            comppowerball = rand() % 31 + 1;
            cout << comppowerball << endl;
        }

        for (int x = 0; x <= 5; x++)
        {
            if ((nums[0] == compnums[x]) && (nums[1] == compnums[x]) && (nums[2] == compnums[x]) && (nums[3] == compnums[x]) && (nums[4] == compnums[x]) && (nums[5] == compnums[x]) && (powerball == comppowerball))
            {
            win5p = true;
            }
            if ((nums[0] == compnums[x]) && (nums[1] == compnums[x]) && (nums[2] == compnums[x]) && (nums[3] == compnums[x]) && (nums[4] == compnums[x]) && (nums[5] == compnums[x]))
            {
            win5 = true;
            }
            // I get lost right here. I don't even know if I'm doing any of this correctly.
        }
    }

Да. Если бы кто-нибудь мог помочь мне, я была бы самой счастливой девушкой. У меня 10, а не 10 000 билетов, поэтому мне легче просматривать их при отладке, но это будет 10 000, когда я закончу. Соответствующие номера лото могут быть в любом месте, поэтому, если числа [1] совпадают с номерами [4], они все равно будут считаться совпадающими. Пожалуйста, задавайте любые вопросы, которые вам нужно задать, я знаю, что это большой беспорядок.

1 Ответ

0 голосов
/ 16 ноября 2018
#include <iostream>
#include<cstdlib>
#include <time.h>
using namespace std;
int main() {
    srand(time(0));
    int count=0;
    bool powerball=false; // to check if powerball matches
    int luckyarr[5];      // the winning numbers
    int luckypb=rand()%30+1; //lucky pb
    cout <<"Showing result for only won Lottos\n";
    cout <<"Lucky numbers: ";
    for(int i=0;i<5;i++){
        luckyarr[i]=rand()%70+1;
        if(i>0){                            // line 15-22 checking if the number already exists or not
            for(int m=0; m<i;m++){
                if(luckyarr[i]==luckyarr[m]){   // it compares the new number with all previous values
                    i-=2;                       // reduces i by 2 so that i++ in line 13 will make the difference =1 and new num will be generated in place of the previous num which was same
                    break;
                }
            }
        }
        cout <<luckyarr[i] << " ";
    }
    cout << "Powerball: "<<luckypb<<endl;
    cout <<"Showing result for only won Lottos\n";
    int arr[5];
    int pb;
    for(int j=1; j<=1000;j++){   //running loop 10000 times as we are buying 10,000 tickets
        for(int i=0; i<5;i++){
            arr[i]=rand()%70+1;      // generating numbers for jth ticket
            if(i>0){
                for(int m=0; m<i;m++){    //chechking similar numbers
                    if(luckyarr[i]==luckyarr[m]){
                        i-=2;
                        break;
                    }
                }
            }
            for(int k=0;k<5;k++){
                if(arr[i]==luckyarr[k]) count++;   // counting the number of matching numbers of jth ticket
            }
        }
        pb=rand()%30+1;
        if(pb==luckypb){
            powerball=true;                 // checking if pb matches
        }

        if(count>0 || powerball){
            cout << "For Lotto no."<< j<< ": ";
            switch (count) {
                case 5:
                    if(powerball) cout <<"You have won the Jackpot.\n";
                    else cout << "You have won $1,000,000\n";
                    break;
                case 4:
                    cout <<"You have won $50,000\n";
                    break;
                case 3:
                    cout <<"You have won $7\n";
                case 2:
                    if(powerball) cout <<"You matched 2 number and powerball and have won $3\n";
                    break;
                case 1:
                    if(powerball) cout <<"You matched only 1 number powerball and have won $3\n";
                    break;
                case 0:
                    if(powerball) cout <<"You matched only powerball and You have won $3\n";
                    break;
            }
            count=0;          // resetting count and pb for next ticket
            powerball=false;
        }


    }


    return 0;
}
...