What is the most efficient way to test for duplica

2019-07-09 01:06发布

I am trying to write a simulator for playing the Powerball lottery, where the program would ask for 5 numbers (aka the white balls) and be inputted into a 6 element array and another number (the red Powerball) into the 6th element. I need to figure out how to test for duplicates in the first 5 elements but the 6th doesn't need to be unique.

I have a loop that I thought would work but it doesn't even execute and is rather messy.

Is there a more efficient way to test for duplicates, maybe involving a bool flag?

const int PBALLAMOUNT = 6;
const int PBMAX = 69;
const int PBMIN = 1;
const int REDMAX = 26;

cout << "Enter the numbers you want to use for the white powerballs" << endl;
for (int k = 0; k < PBALLAMOUNT - 1; k++)
{
    cin >> pBallNums[k];
    while (pBallNums[k] < PBMIN || pBallNums[k]>PBMAX) 
    {
        cout << "Invalid input! Please enter different numbers between 1 and 69" << endl;
        cin >> pBallNums[k];
    }
}

bool dup = false;
for (int i = 0; i < PBALLAMOUNT - 1; i++) 
{
    for (int j = i + 1; j < PBALLAMOUNT - 1; j++) 
    {
        while (!dup) 
        {
            if (pBallNums[i] == pBallNums[j]) 
            {
                cout << "Please enter a unique number from the others in your PowerBall number selection" << endl;
                cin >> pBallNums[i];
            }

        }
    }
}

cout << "And what would you like for your redball" << endl;
cin >> pBallNums[5];

while (pBallNums[5] < PBMIN || pBallNums[5] > REDMAX)
{
    cout << " The red powerball needs to be between 1 and 26: ";
    cin >> pBallNums[5];
}

I basically just need it to alert the user if they have already entered a number into the array and offer another std::cin >> pBallNums statement but the actual result is just nothing happens after you enter the numbers.

3条回答
男人必须洒脱
2楼-- · 2019-07-09 01:16

Sort, then check adjacent items for equality.

查看更多
乱世女痞
3楼-- · 2019-07-09 01:22

First, try not to mix real requirements with implementation details.

[...] where the program would ask for 5 numbers (aka the white balls) and be inputted into a 6 element array and another number (the red powerball) into the 6th element.

What you really want is: 5 distinct numbers taken from user input. And from your code I read that the check should happen after each single input. I suppose reading the last number is fine, so lets leave that aside.

Next, get acustomed to the containers in the standard library. They are not many in numbers but what you can do with them is uncountable. To have distinct elements in a container you want either std::unsorted_set or std::set. Then basically all you need is to use insert:

#include <set>
#include <iostream>
int main()
{
    std::set<int> numbers;
    auto x = numbers.insert(1);
    std::cout << "1 was not present before? : " << x.second << "\n";
    x = numbers.insert(1);
    std::cout << "1 was not present before? : " << x.second << "\n";
}

prints:

1 was not present before? : 1
1 was not present before? : 0
查看更多
Juvenile、少年°
4楼-- · 2019-07-09 01:31

"I basically just need it to alert the user if they've already entered a number into the array and offer another cin >> pBallNums statement."

In that case simply use std::set and use it's std::set::emplace method to store the user input into the set.

From cppreference.com,

template< class... Args >
std::pair<iterator,bool> emplace( Args&&... args );

std::set::emplace

Returns a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place. True for Insertion, False for No Insertion.

Simply take this information for your case and loop again for the next user input.


Here is a sample code (See Live):

#include <iostream> 
#include <set>

int main()
{
    std::set<int> mySet;

    for(int loopCount{ 0 }; loopCount != 5; )// repeat until maximum loop count
    {
        int input; std::cin >> input; // user input
        // get the std::pair<iterator,bool>
        const auto pairIter = mySet.emplace(input);
        // increment maximum loop count, if insertion successful
        if (pairIter.second) ++loopCount;
    }
    for (const int userInput : mySet)
        std::cout << userInput << " ";

    return 0;
}

Sample input:

1
1
2
3
4
2
4
3
5

Output:

1 2 3 4 5
查看更多
登录 后发表回答