【问题标题】:What is the most efficient way to test for duplicates in a user inputted array?在用户输入的数组中测试重复项的最有效方法是什么?
【发布时间】:2019-09-25 12:16:06
【问题描述】:

我正在尝试编写一个模拟器来玩 强力球彩票,其中程序会要求输入 5 数字(又名白球)并输入到 6 元素数组中,然后另一个数字(红色强力球)进入6th 元素。 我需要弄清楚如何测试第一个 5 元素中的重复项,但 6th 不需要是唯一的。

我有一个我认为可以工作的循环,但它甚至没有执行,而且相当混乱。

是否有更有效的方法来测试重复项,可能涉及 bool 标志?

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

我基本上只需要它来提醒用户是否已经在数组中输入了一个数字并提供另一个std::cin &gt;&gt; pBallNums 语句,但实际结果在你输入数字后什么都没有发生。

【问题讨论】:

  • 什么是pBallNums?数组还是std::vectorpBallNums 中值的顺序是否重要?
  • std::any_of(和朋友)似乎是一个显而易见的工具……
  • 考虑使用std::set。保证套装中的所有物品都是独一无二的。
  • 对收到的set 评论的好评感到惊讶。对于最多大小为 4 的列表,@JesperJuhl 的宣传会更有效率
  • @JeJo Neat 网站。以后得玩这个了。我所看到的是,对于基准测试来说,两者都执行得太快了。对于一些条目运行一次,这是意料之中的。

标签: c++ arrays algorithm duplicates user-input


【解决方案1】:

“我基本上只需要它来提醒用户,如果他们已经输入 将一个数字放入数组并提供另一个 cin &gt;&gt; pBallNums 语句。"

在这种情况下,只需使用 std::set 并使用它的 std::set::emplace 方法将用户输入存储到集合中。

来自cppreference.com

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

std::set::emplace

返回由插入元素的迭代器组成的对,或者返回已经存在的元素如果没有插入 发生了,一个布尔值表示插入是否发生。真的 插入,不插入则为假

只需为您的案例获取此信息,然后再次循环以获取下一个用户输入。


这是一个示例代码(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;
}

示例输入

1
1
2
3
4
2
4
3
5

输出

1 2 3 4 5

【讨论】:

    【解决方案2】:

    排序,然后检查相邻项目是否相等。

    【讨论】:

      【解决方案3】:

      首先,尽量不要将实际需求与实现细节混为一谈。

      [...] 程序会要求 5 个数字(又名白球) 并被输入到一个 6 元素数组和另一个数字(红色 强力球)进入第 6 个元素。

      您真正想要的是:从用户输入中提取的 5 个不同的数字。从您的代码中,我读到检查应该在每次输入之后进行。我想读最后一个数字是好的,所以让我们把它放在一边。

      接下来,习惯containers in the standard library。它们的数量并不多,但你可以用它们做什么是不可数的。要在容器中拥有不同的元素,您需要std::unsorted_setstd::set。 那么基本上你只需要使用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";
      }
      

      打印:

      1 was not present before? : 1
      1 was not present before? : 0
      

      【讨论】:

        猜你喜欢
        • 2013-04-28
        • 2016-10-26
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        • 2020-07-10
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        相关资源
        最近更新 更多