初学者制作不含cin的乐透程序,不知道如何比较所有数字

时间:2018-11-16 01:45:07

标签: c++ arrays if-statement variable-assignment

这是我的作业说明:

  

您将编写一个可以进行乐透的程序。

     

乐透由1至70的5个数字和1至70的强力球组成   数字1-30。

     

前5个数字不应重复(中奖数字相同)。   强力球可以重复前5个数字中的任何一个。

     

您将购买10,000张乐透彩票。每张票有6张   数字(5个数字和1个战俘)。

     

给每张彩票随机数字,并与中奖数字进行比较   (获奖号码仅生成一次)。

     

匹配5个数字和强力球号码,您将赢得大奖!

     

仅匹配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张。匹配的乐透号码可以在任何地方,因此,如果number [1]与compnumbers [4]相匹配,则仍将被视为匹配。请问任何您需要问的问题,我知道这很麻烦。

1 个答案:

答案 0 :(得分:0)

#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;
}