当前位置:网站首页>Concept and application of logarithm
Concept and application of logarithm
2022-04-22 16:58:00 【Starlight technologist】

- Generates an array of random numbers of random length
#include <iostream>
#include <time.h>
#include <cstdlib>
#include<vector>
using namespace std;
vector<int> get_random_arr(int maxlen, int maxnum)
{
srand((unsigned)time(NULL));
int L = rand() % maxlen;
cout << "L=" << L << endl;
vector<int> Arr(L, 0);
for (int i = 0; i < Arr.size(); i++)
{
Arr[i] = (rand() % (maxnum+1)) - (rand() % (maxnum+1));//[0,max_num]-[0,max_num]
}
return Arr;
}
int main()
{
int max_len = 10;
int max_num = 10;
vector<int> res = get_random_arr(max_len, max_num);
cout << res.size() << endl;
for (int i = 0; i < res.size(); i++)
{
cout << res[i] << " ";
}
return 0;
}
- c++ Generate random numbers
To generate random numbers within a specified range , You can use it first rand() The function produces a [0,RAND_MAX] Random numbers in the range , Then, after the transformation to the specified range .
produce [a,b) The random number , have access to (rand() % (b-a))+a;
produce [a,b] The random number , have access to (rand() % (b-a+1))+a;
produce (a,b] The random number , have access to (rand() % (b-a))+a+1;
General formula :a+rand() % n;
among :a Is the starting position of the range ,n Is the range of integers
produce [a,b] The random number , have access to a+(int)b*rand()/(RAND_MAX+1);
produce [0,1] Floating point number , have access to rand()/double(RAND_MAX)
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
cout <<"RAND_MAX:"<< RAND_MAX << endl;
srand((unsigned)time(NULL));
for (int i = 0; i < 5; i++)
cout << (rand()%2)<<" "; // Generate [0,1] Random numbers in the range
cout << endl;
for (int i = 0; i < 5; i++)
cout << (rand() % 5 + 3) << " "; // Generate [3,7] Random numbers in the range
cout << endl;
}
- Application of logarithm
To make sure your code is completely correct , Not just on some use cases , You need a logarithm to judge ; Generate a random array , Then throw into the selection sort and bubble sort respectively , Compare the results , If the results correspond to different , Then two codes should be good, one , Or it's all wrong ;
Set the number of comparisons , If say 50 Ten thousand times , Each time a random array is generated , Put it into two sorting algorithms , Compare the results returned by the two algorithms ; If the results of each comparison are equal , It shows that both algorithms are correct , If you return false, It depends on the correctness of the algorithm ; It can also correspond to the language algorithm api Compare ;
- code
#include <iostream>
#include <time.h>
#include <cstdlib>
#include<vector>
using namespace std;
// Insertion sort , Ascending
vector<int> get_insert(vector<int>& Arr)
{
for (int i = 1; i < Arr.size(); i++)
{
for (int j = i - 1; j >= 0 && Arr[j] > Arr[i]; j--)
{
swap(Arr[j], Arr[i]);
}
}
return Arr;
}
// Selection sort
vector<int> get_sort(vector<int>& Arr)
{
for (int i = 0; i < Arr.size(); i++)
{
int minidx = i;
for (int j = i + 1; j < Arr.size(); j++)
{
minidx = (Arr[minidx] < Arr[j] ? minidx : j);
}
swap(Arr[i], Arr[minidx]);
}
return Arr;
}
vector<int> get_random_arr(int maxlen, int maxnum)
{
srand((unsigned)time(NULL));
int L = rand() % maxlen;
vector<int> Arr(L, 0);
for (int i = 0; i < Arr.size(); i++)
{
Arr[i] = (rand() % (maxnum+1)) - (rand() % (maxnum+1));//[0,max_num]-[0,max_num]
}
return Arr;
}
bool compare(vector<int>& A, vector<int>& B)
{
if (A != B)
return false;
return true;
}
int main()
{
int max_len = 100;
int max_num = 100;
int test_num = 100;
int res_flag = true;
for (int i = 0;i < test_num ; i++)
{
vector<int> rand_arr1 = get_random_arr(max_len, max_num);
vector<int> rand_arr2 = rand_arr1;
vector<int> res1 = get_insert(rand_arr1);
vector<int> res2 = get_insert(rand_arr2);
if (!compare(res1, res2))
{
cout << "fasle" << endl;
break;
}
}
cout << "success" << endl;
return 0;
}
版权声明
本文为[Starlight technologist]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221653092956.html
边栏推荐
- Test life | less than 2 years after graduation, 0 experience and won the 30W annual salary of a well-known Internet enterprise. How did he do it?
- 抗疫神器:健康码、行程码自动识别!
- AQS source code reading
- 员工健康码数据如何自动汇总?
- 在文件夹中查找字符串
- numpy基础大全(创建、索引、常用函数)
- 中金证券开户app叫什么?股票开户怎么样佣金优惠又安全?
- Force deduction solution summary 396 rotation function
- Gome retail takes advantage of the east wind of digital economy to create a "boat body" consumption experience
- 刘畊宏是抖音的钻头
猜你喜欢

SDN学习之Opendaylight浅析(三)
![[unity] make aim IK 1 with animation rig](/img/52/b8ee9d0982a8671356954fbbad775d.png)
[unity] make aim IK 1 with animation rig

Knowing that it is listed in Hong Kong, it is the first Chinese concept stock to return to Hong Kong with dual main listing
![[appium] simple transplantation of unittest framework of Youdao cloud app and design of driver file](/img/d0/1ad6f79c9ae9f9e2d50495c2f37efb.png)
[appium] simple transplantation of unittest framework of Youdao cloud app and design of driver file

A serial port data receiving mode of stm32

VSCode插件打包迁移与指定位置

Shiro cache management

Respectful and modest

GlboalMapper20 如何一次性把上百个经纬度投影的影像转为国家2000或者其他投影的影像

Multithreading uses redis to accumulate. The result is incorrect. Solution
随机推荐
红杉中国带队,投了两位女创始人
[Dahua cloud native] wonderful relationship between message queue and express cabinet (with video)
【滤波与卷积(二)】
SDN学习之Opendaylight浅析(五)
frp反向代理
FRP reverse proxy
电商价格数据监测接口/品牌商品控价接口/商品数据分析接口/比价搜索API接口,超详细的接口对接说明
Leetcode problem brushing plan -- monotonic sequence
leetcode:451. Sort by character frequency
Update description of the latest process engine flowable 6.7.2
Facing the global market, platefarm today logs in to four major global platforms such as Huobi
正则匹配URL
SaaS 长河下,AfterShip 技术升级的“加减法”
ASP.NET Core实现JWT授权与认证(2.实战篇)
ifconfig、route、ip route、ip addr、 ip link 用法
Linux Mysql 8 修改密码 Your password does not satisfy the current policy requirements
最新流程引擎 flowable 6.7.2 更新说明
[filtering and convolution (II)]
list转map 及Duplicate key.md
Blue Bridge Cup exercise 016