如何在c++中检查数组中的重复数字
How do check an array for repeat numbers in c++?
我正试图用4个随机生成的[srand-sseed rand()]生成一个数组,而不需要任何重复。我正在使用for循环:
在阵列中选择一个位置,生成一个数字,将编号分配给当前突出显示的位置根据以下伪代码,检查分配的编号是否与前一个条目不相等。
if no -
Then select the next position in the array and generate a new number
if yes -
Do not move to the next array position and generate a new number again.
repeat until array position 3
这是我的尝试:
int operator_selection;
int operator_index[3];
int random_value;
for (operator_selection = 0; operator_selection < 4; operator_selection++)
{
random_value = rand() %4 + 1;
if (random_value = operator_index[0] || operator_index[1] || operator_index[2])
{
(operator_selection - 1);
}
operator_index[operator_selection] = random_value;
cout<<operator_index[operator_selection]<<" ";
if (operator_selection == 3)
{
cout<<endl;
}
}
然而,当我运行可执行文件时,我总是以重复结尾,所以我很确定我的第一个"if语句"背后的逻辑是有缺陷的。
我是一个c++初学者,这是我第三次尝试从头开始编写源文件,所以如果我犯了一个愚蠢的错误,我很抱歉。
我在您发布的代码中看到了一些问题。
问题1
线路
if (random_value = operator_index[0] || operator_index[1] || operator_index[2])
不做你希望做的事。你需要使用:
if ( (random_value == operator_index[0]) ||
(random_value == operator_index[1]) ||
(random_value == operator_index[2]) )
问题2
将random_value
与operator_index[0]
、operator_index[1]
与operator_index[2]
进行比较是不正确的。您只需要与operator_index[operator_selection-1]
进行比较。
问题3
线路
(operator_selection - 1);
不会改变CCD_ 6的值。它只是计算表达式并丢弃值。
您需要的是一个递减operator_selection
值的语句。例如
--operator_selection;
问题4
当您找到一个现有值时,您需要继续循环的下一次迭代。
以下是循环的更新版本:
for (operator_selection = 0; operator_selection < 4; operator_selection++)
{
random_value = rand() %4 + 1;
bool matchFound = false;
for ( int i = 0; i < operator_selection-1; ++i )
{
if ( random_value == operator_index[i] )
{
matchFound = true;
break;
}
}
if ( matchFound )
{
--operator_selection;
continue;
}
operator_index[operator_selection] = random_value;
cout<<operator_index[operator_selection]<<" ";
}
// Move this out of the loop.
cout<<endl;
以下是使用std::array
和std::random_shuffle
的版本:
#include <iostream>
#include <array>
#include <algorithm>
#include <random>
int main()
{
std::array<int, 4> a = {1, 2, 3, 4};
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(a.begin(), a.end(), g);
for(auto& i : a)
std::cout << i << " ";
}
实时演示
这个版本可读性更强,效率更高。
更新:这并不能直接回答问题,如果是在家工作,也不适合。但我会把它留在这里,以防OP对更好的选择感兴趣。
首先,您需要使数组变大。如前所述,使用std::数组会更好,但我将坚持使用旧式数组。定义尺寸为3的operator_index
时,只允许使用3个元素(偏移量从0到2)。所以这需要一个4的维度。
您还应该初始化数组的内容(或者确保从不从未初始化的元素中读取)。
接下来,对于第一个随机数,不存在冲突的可能性。所以你可以直接把它放到数组中。
operator_index[0] = rand() %4 + 1;
然后,对于剩下的3个条目,您可以从1循环到3。
你甚至可以更进一步。当你填写了前3个条目时,你可以直接计算最后一个,
operator_index[3] = 10 - operator_index[2] - operator_index[1] - operator_index[0];
(从1到4的总和是10,所以最后一个元素是10-前三个元素的总和)
你的代码的主要问题是这个
if (random_value = operator_index[0] || operator_index[1] || operator_index[2])
{
(operator_selection - 1);
}
这是一个赋值,而不是相等性检查。它被分配了前3个元素的逻辑或。由于您没有初始化数组,您将读取垃圾,结果可能是random_value
将被设置为1,并且条件将计算为true。
(operator_selection - 1)
是一种无副作用的算子。它不修改operator_selection
。此外,一旦您发现重复,您需要重新开始循环。
这是一个最小化循环的版本。
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int operator_selection;
int operator_index[4] = {0};
int random_value;
srand(time(0));
operator_index[0] = rand() %4 + 1;
for (operator_selection = 1; operator_selection < 3; operator_selection++)
{
random_value = rand() %4 + 1;
if (operator_index[0] == random_value || operator_index[1] == random_value)
{
--operator_selection;
continue;
}
operator_index[operator_selection] = random_value;
}
operator_index[3] = 10 - operator_index[2] - operator_index[1] - operator_index[0];
for(auto& elem : operator_index)
std::cout << elem << " ";
std::cout << "n";
}
尽管如此,我仍然更喜欢std::random_shuffle方法,我也建议这样做。
另一个技巧是,如果条件不满足,则将lopp后退一步。
#include <iostream>
#include <ctime>
using namespace std;
int main(void)
{
const int size=100 ;
int arr[100] ;
int i=0;
srand(time(0));
for ( i=0;i<size;i++) {
arr[i]=rand() % size;
for(int j=0; j < i ; j++) if (arr[j] == arr[i]) i--;
}
cout<<" nnn ";
// Loop to display the array arr[ ]
for ( i=0;i<size;i++) cout<<""<<arr[i]<<"t";
cout<<" nPress any key to continuen";
cin.ignore();
cin.get();
return 0;
}
输出:
91 71 14 65 12 25 64 98 83 28
99 9 5 0 89 36 95 55 73 90
78 2 52 70 39 63 17 50 7 58
34 84 40 51 20 31 38 32 35 49
61 66 72 92 6 59 41 13 22 23
81 56 1 16 21 62 57 10 11 54
77 86 76 93 4 96 8 33 94 67
29 48 15 82 97 37 26 46 43 80
68 85 60 30 42 53 18 69 45 88
47 79 75 44 24 27 74 3 19 87
Press any key to continue
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 数组/c++中的大量数字
- 遍历并行数组以确定C++中的最大数字
- 整数区间(或 int 数组)中每个数字的出现次数
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 获取 2D 数组 c++ 中的所有数字对
- 为什么使用数组元素查找最大数字的程序不起作用?
- 如何从向量或数组中选择最常见的数字?(前五名)C++
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 以十为基数的数字到布尔数组,该数组要求二进制/基数为 2 的数字
- C++ 检查结果数组中有多少次数字
- 如何在不使用 C++ 中的数组或函数的情况下查找 N 位数字的所有排列
- 如何在C++中有效地将数字值重新分配给字符数组
- 查找数组中的最小数字
- 查找数组中重复(重复)数字的索引
- 我必须找到给定数组中所有数字的周期,就像有很多解决方案,但数组的大小是 10^5
- 类、数字/数组或对象中的对象