非重复随机数 2D 向量 (C++)
Non repeating Random Numbers 2D Vector (C++)
>EDIT:我目前正在做一个项目。我几乎完成了,尽管我似乎无法弄清楚如何用非重复的随机数填充我的向量(矩阵(。我得到的是随机数,但其中一些是重复的。我尝试实现一种方法来检查推送到向量的数字是否已经存在,但这不起作用。
有没有简单的方法可以解决这个问题?
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
/*
The purpose of this program is to create a matrix using 2D vectors, then decide
if it is a perfect matrix or not.
*/
void matrix(int );
int main()
{
int input;
char again = 'y';
cout << "Welcome to my perfect matrix program. The function of the program is"
"nto:"
"n"
"n 1. Allow the user to enter the size of the perfect matrix, such as"
"n N. N>=2."
"n 2. Create a 2 D vector array of size N x N."
"n 3. Populate the 2 D vector array with distinct random numbers."
"n 4. Display the sum for each row, column, and diagonals then"
"n determine whether the numbers in N x N vector array are perfect"
"n matrix numbers.";
cout << endl << endl;
while(again == 'y' || again == 'Y')
{
cout << "nEnter the size of the matrix : ";
cin >> input;
while(input < 2)
{
cout << "nError *** Incorrect input - You entered a number < 2"
"nEnter a Positive integer Number >= 2: ";
cin >> input;
}
matrix(input);
cout << "nWould you like to find another perfect matrix?"
"nEnter y || Y for yes or n || N for no: ";
cin >> again;
if(again == 'N' || again == 'n')
{
cout << "nThis perfect matrix algorithm is implemented by B"
"nFebruary 13th - 2017n";
}
while(again != 'Y' && again != 'y' && again != 'N' && again != 'n')
{
cout << "nError *** Invalid choice - Must enter y|Y or n|N" << endl;
cout << "nWould you like to find another perfect matrix?"
"nEnter y || Y for yes or n || N for no: ";
cin >> again;
}
}
return 0;
}
void matrix(int value)
{
int N = value;
bool isPerf = true;
int sum = 0,
i,
j;
int diagSum1 = 0,
diagSum2 = 0;
vector<vector<int> >arr;
srand(time(0));
for(i = 0; i < N; i++)
{
vector<int> temp;
bool check;
int digit;
for(j = 0; j < N; j++)
{
do
{
digit = rand()%8;
check = true;
for(int k = 0; k < j; k++)
{
if(digit == temp.front())
{
check = false;
break;
}
}
}while(!check);
temp.push_back(digit);
}
arr.push_back(temp);
}
cout << endl;
cout << "The perfect matrix that is created for size " << value << ":";
cout << endl << endl;
for(i = 0; i < arr.size(); i++)
{
for(j = 0; j < arr[i].size(); j++)
{
cout << arr[i][j];
cout << " ";
}
cout << endl << endl;
}
for(i = 0; i < arr.size(); i++)
{
for(j = 0; j < arr[i].size(); j++)
{
sum += arr[i][j];
}
}
int perf = sum/3;
cout << "The perfect number is: " << perf << endl << endl;
for(i = 0; i < arr.size(); i++)
{
int rowSum = 0;
for(j = 0; j < arr[i].size(); j++)
{
rowSum += arr[i][j];
}
cout << "Sum of numbers in Row # " << i + 1 << " = " << rowSum;
if(rowSum != perf)
{
isPerf = false;
}
cout << endl;
}
cout << endl;
for(i = 0; i < arr.size(); i++)
{
int colSum = 0;
for(j = 0; j < arr[i].size(); j++)
{
colSum += arr[j][i];
}
cout << "Sum of numbers in Column # " << i + 1 << " = " << colSum;
if(colSum != perf)
{
isPerf = false;
}
cout << endl;
}
cout << endl;
for(i = 0; i < arr.size(); i++)
{
diagSum1 += arr[i][i];
diagSum2 += arr[i][arr.size() - i - 1];
}
cout << "Sum of numbers in first diagonal = " << setw(3) << diagSum1 << endl;
if(diagSum1 != perf)
{
isPerf = false;
}
cout << "Sum of numbers in second diagonal = " << setw(3) << diagSum2 << endl;
if(diagSum2 != perf)
{
isPerf = false;
}
cout << endl;
if(isPerf)
{
cout << "The above is a perfect matrix" << endl;
}
else
cout << "The above is not a perfect matrix" << endl;
cout << endl;
}
一种选择是创建一个临时集合,供您使用随机数填充。每当插入元素时,请检查集合的大小,当大小等于所需的不同元素数时,循环访问集合以检索值并填充矩阵。
...
std::set<int> distinct;
while (distinct.size() < input) {
int r_num = /* random number generation */
distinct.insert(r_num);
}
std::set<int>::iterator it;
for (it = distinct.begin(); it != distinct.end(); it++) {
/* method of adding to matrix */ = *it;
}
...
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么