学习左程云算法教程,需要制作对数器
- 制作一个长度可变的,在一定range之内的随机数组发生器
// for test
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * (maxValue + 1));
}
return arr;
}
cpp的random
#include <stdlib.h>
#include <iostream>
int v1 = rand() % 100; // v1 in range 0 to 99
int v2 = rand() % 10 + 1; // v2 in range 1 to 10
void generateRandomArray(int maxSize, int maxValue, int* array, int & size)
{
size = rand() % (maxSize+1); // 0---maxSize之间
array=new int[size];
std::cout<<"size="<<size<<"---";
for (int i = 0; i<size;i++)
{
array[i]=rand() % (maxValue+1);// 0---maxValue之间
std::cout<<array[i]<<" ";
}
}
int main()
{
int * array;
int size;
generateRandomArray(5,8,array,size);
generateRandomArray(5,8,array,size);
}
// size=3---7 5 7 size=4---3 6 1 5
- isEqual用来比对结果
// for test
public static boolean isEqual(int[] arr1, int[] arr2) {
if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
return false;
}
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
cpp版
bool isEqual(int* arr1, int size1, int* arr2, int size2)
{
if (arr1 == nullptr && arr2!=nullptr) ||
arr1 !=nullptr && arr2==nullptr){
return false;
}
if (arr1 == nullptr && arr2 == nullptr){
return true;
}
if (size1 != size2){
return false;
}
for (int i = 0; i < size1; i++)
{
if (arr1[i] != arr2[i]){
return false;
}
}
return true;
}