彩票计划(升序,非重复等)

时间:2016-12-13 16:20:42

标签: c++

我在制作乐透计划时遇到了麻烦。我最近进入编程,所以我不太了解它。 所以这个人应该能够选择他想要的行数。然后应该出现1-35之间的7个随机数,按升序排列并且在该行中不重复(它可以再次出现在下一行)。 我使用rand / srand来制作"随机"号。

任何帮助都会很好! 提前致谢! :)

这就是代码看起来像atm。

void randNr(int[]);
int main()
{
    int rad;
    string svar, igen;
    setlocale(LC_ALL, "");


    cout << "Hur många rader vill du ha? Välj ett tal mellan 1-12." << endl;            //How many rows do you want?
    cin >> rad;
    for (int a = 1; a < 7; a++)
    {
        if (rad < 0 || rad > 12)
        {
            if (cin.fail())
            {
                system("CLS");
                cin.clear();
            }
            cout << "Fel inmatning, Välj ett av alternativen. \n" << endl;          //Wrong answer, choose one of the alternatives
            cout << "För att börja om tryck 'j'." << endl;                          //'j' to start over
            cout << "För att avsluta tryck 'n'." << endl;                           //'n' to quit program
            cin >> svar;

            if (svar == "j" || svar == "J" || svar == "Ja" || svar == "ja")
            {
                system("CLS");
                return main();
            }
            else if (svar == "n" || svar == "N" || svar == "Nej" || svar == "nej")
            {
                return 0;
            }
            else if (svar != "j" && svar != "J" && svar != "Ja" || svar != "n" && svar != "N" && svar != "nej" && svar != "Nej")
            {
                system("CLS");
            }

        }
    }
    randNr(tal);

while (true) {

        cout << "\nFör att köra igen: tryck 'j'" << endl;           //To play again press 'j'
        cout << "För att avsluta  : tryck 'n'" << endl;             // To quit, press 'n'
        cin >> igen;
        if (igen == "j" || igen == "J" || igen =="Ja" || igen == "ja")                          
        {
            system("CLS");
            return main();
        }
            else if(igen == "n" || igen == "N" || igen == "nej" || igen == "Nej" )
            {
                system("CLS");
                return 0;
            }
                else
                {
                    system("CLS");
                    cout << "Fel inmatning, välj ett av alternativen" << endl;  //Wrong answer, choose on of the alternatives
                }           
    }
    system("pause");
    return 0;
}
void randNr(int tal[])
{
    //DONT KNOW HOW TO DO THE ASCENDING / NON REPEATING PART 
    //---------------------HERE---------------------------      
}

1 个答案:

答案 0 :(得分:0)

此代码旨在提供有益的帮助并帮助您学习,并不像其他评论所说的那样最佳:

void randNr(int tal[]) {
    int count = 0;
    while (count < SIZE) {
        int number = rand() % 35 + 1;  // range [1, 35]

        int index = 0;
        // find where the number should be placed
        while (index < count && tal[index] < number) {
            index++;
        }

        // if it is not a duplicate, shift other numbers up, then add it
        if (tal[index] != number) {
            for (int i = count; i >= index; i--)
                tal[i + 1] = tal[i];
            tal[index] = number;
            count++;
        }
    }
}