从随机生成的数组中删除重复的数字
remove duplicate numbers from randomly generated array
我正在学习c++,并且遇到了一个关于如何从随机生成的值从0到100的整数数组中删除重复项的想法。我这样编码,但我看到一些数字是重复的。我真搞不懂这个臭虫。请帮助。
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int item[100];
int z;
int count = 0;
for (z = 0; z < 100; z++) {
item[z] = rand() % (z + 1);
count++;
cout << item[z] << endl;
}
cout << "Total = ";
cout << count << endl;
int i, j;
int NewLength = 1;
for (i = 1; i < 100; i++) {
for (j = 0; j < NewLength; j++) {
if (item[i] == item[j])
break;
}
if (j == NewLength)
item[NewLength++] = item[i];
}
for (int x = 0; x < 100; x++) {
cout << item[x] << endl;
}
}
如果你可以使用c++ 11并且不介意这样做,你可以做的是将数字0到100存储在一个数组。
接下来你可以随机shuffle
,瞧…由0到100之间的随机数组成的数组。否则,如果不能,请忽略此解决方案。
#include <random>
#include <numeric>
#include <algorithm>
#include <iostream>
int main()
{
int arr[100] = {0};
std::iota(&arr[0], &arr[100], 0);
std::random_device rd;
std::mt19937 mt(rd());
std::shuffle(&arr[0], &arr[100], mt);
for (int i = 0; i < 100; ++i)
std::cout<<arr[i]<<" ";
}
另一种选择是生成一个0到100的数组,并随机交换索引(假设您不能使用c++ 11和random_shuffle
)。
从100个整数的数组开始。假设其中一些整数是重复的,则的唯一整数少于 100个。因此,由于数组中的唯一整数少于100个,除非算法生成的新整数值不在原始序列中,否则如果打印出数组的所有100个值,就可以肯定会看到重复的值,就像您在程序末尾所做的那样。
如果你只打印到NewLength
,你不应该看到任何重复(假设你的算法是正确的,我没有严格检查)。
for (int x = 0; x < NewLength; x++) {
cout << item[x] << endl;
}
#include <ctime>
int main()
{
srand(time(NULL));
int item[100];
int z;
int count = 0;
for (z = 0; z < 100; z++) {
item[z] = rand() % (z + 1);
count++;
cout << item[z] << endl;
}
cout << "Total = ";
cout << count << endl;
//Assume if duplicate found assign -1 in the place of duplicate
for(int i = 0;i<100;++i)
{
if(item[i] == -1) //If this number already checked & it is a duplicate
continue;
for(int j=i+1;j<99;++j)
{
if(item[i] == item[j])
{
//Duplicate found.
item[j] = -1; //Assign -1 in the place of duplicate
}
}
}
for (z = 0; z < 100; z++) {
if(item[z] != -1)
cout << item[z] << endl;
}
char c;
cin >> c;
return 0;
}
这是另一个"从随机生成的整数数组中删除重复项"。
如果遇到重复的随机数,下面这行代码将反转一次迭代。
for(int j=0;j<i;j++) if (random_once[j]==random_once[i]) i--;
整个代码:#include <ctime>
#include <iostream>
using namespace std;
int main()
{
int size=100 ;
int random_once[100 ];
srand(time(0));
for (int i=0;i<size;i++)
{
random_once[i]=rand() % 100;
// generate unique random number only once
for(int j=0;j<i;j++) if (random_once[j]==random_once[i]) i--;
}
cout<<" "<< i <<"nnn ";
for ( i=0;i<size;i++) cout<<" "<<random_once[i]<<"t";
return 0;
}
输出: 60 61 96 77 89 94 31 87 19 24
27 99 64 7 8 88 62 75 37 97
28 55 92 48 91 47 50 13 36 85
21 34 82 23 40 4 79 90 46 10
18 0 2 74 56 33 70 84 49 73
69 57 32 41 83 67 76 43 35 44
80 14 25 93 11 15 63 9 26 16
5 98 17 38 51 30 53 58 3 65
66 52 95 86 78 29 42 12 45 59
81 71 39 6 1 68 20 72 54 22
Press any key to continue
相关文章:
- 如何在 getline 中删除/忽略超过特定数字的字符?
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- C++ 从 std::string 中删除所有非数字字符
- 如何删除文件中的数字,通过 c++ fstream 的
- 删除C++中向量中第一个最小的数字(并保持顺序)
- 我应该如何使用remove_if删除两个数字范围内的元素
- 取代表开始/结束的数字对,并删除重叠
- 如何从数组中删除重复的数字?
- 删除数字并在C++中保留字符串的下划线
- 无法使用"擦除"函数从 c++ 字符串中删除数字
- C++ 使用数字删除文件 io 行
- 代码问题.使用矢量删除相同的数字,除了它的最后一个入口
- 如何删除n个数字中的C数字
- 删除数组中所有出现的数字 C++
- 使用find_if从向量删除所有偶数数字
- 用数字删除文件
- 我不想要最后一个数字/值后面的逗号,如何删除它(c ++,循环)
- 从控制台获取的每 30 秒输入后,从 Vector 中逐个删除数字
- 从堆栈中删除数字
- 删除<数字>在O(n)时间内来自容器的元素的数量