从随机生成的数组中删除重复的数字

remove duplicate numbers from randomly generated array

本文关键字:删除 数字 数组 随机      更新时间:2023-10-16

我正在学习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