当前位置:网站首页>对数器的概念和使用
对数器的概念和使用
2022-04-22 16:53:00 【星光技术人】

- 产生随机长度随机数值的数组
#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++产生随机数
要产生指定范围内的随机数,可以先使用rand()函数产生一个[0,RAND_MAX]范围内的随机数,然后在变换到指定范围内。
产生[a,b)的随机数,可以使用 (rand() % (b-a))+a;
产生[a,b]的随机数,可以使用 (rand() % (b-a+1))+a;
产生(a,b]的随机数,可以使用 (rand() % (b-a))+a+1;
通用公式:a+rand() % n;
其中:a为范围起始位置,n为整数的范围
产生[a,b]的随机数,可以使用 a+(int)b*rand()/(RAND_MAX+1);
产生[0,1]的浮点数,可以使用 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)<<" "; //生成[0,1]范围内的随机数
cout << endl;
for (int i = 0; i < 5; i++)
cout << (rand() % 5 + 3) << " "; //生成[3,7]范围内的随机数
cout << endl;
}
- 对数器的应用
为了确保自己的代码完全正确,而不是只是在部分用例上正确,就需要对数器来进行判断;产生一个随机数组,然后分别抛进选择排序和冒泡排序,比较产生的结果,如果结果对应不同,那么两个代码要不错一个,要不全错;
设置比较的次数,假如说50万次,每次都产生一个随机数组,放进两种排序算法中,比较两个算法返回的结果;如果每次比较对应的结果都相等,就说明两种算法都正确,如果返回false,就需要看算法的正确性与否;也可以与语言算法对应的api进行比较;
- code
#include <iostream>
#include <time.h>
#include <cstdlib>
#include<vector>
using namespace std;
//插入排序,升序
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;
}
//选择排序
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;
}
版权声明
本文为[星光技术人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qhu1600417010/article/details/124341397
边栏推荐
- SaaS 长河下,AfterShip 技术升级的“加减法”
- 蕉下在走完美日记的老路
- list转map 及Duplicate key.md
- 华为走在老路上
- Blue Bridge Cup practice 014
- 使用AppCube,零门槛快速搭建5G消息服务号
- C ODBC loads the files of one folder into the blob column of Oracle database and downloads the blob column to another folder
- [unity] make aim IK 1 with animation rig
- Crashsight general function & feature function introduction
- 【面试普通人VS高手系列】请说一下网络四元组
猜你喜欢

STM32的一种串口数据接受方式

Gome retail takes advantage of the east wind of digital economy to create a "boat body" consumption experience

Cognitive series 4: Notes on cognitive breakthrough

FRP reverse proxy

Cvpr2019 domain adaptation / semantic segmentation: adapting structural information across domains for boosting SEMA

Dispute over stock: Gome retail explores the way to break the situation with full retail
![[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

What is the advantage of asemi low voltage drop Schottky diode over ordinary Schottky diode?

蕉下在走完美日记的老路

VSCode插件打包迁移与指定位置
随机推荐
员工健康码数据如何自动汇总?
多线程使用redis进行累加结果不对解决方案
Cvpr2019 domain adaptation / semantic segmentation: adapting structural information across domains for boosting SEMA
Record a redis value, which is not a small production accident you want
Blue Bridge Cup practice 014
【周报】4月17日
【面试普通人VS高手系列】请说一下网络四元组
蓝桥杯练习014
Cognitive series 4: Notes on cognitive breakthrough
SDN学习之Opendaylight浅析(五)
[interview ordinary people vs Expert Series] please talk about the network quadruple
Summary of process and principle of solution to concurrency problem (form repeated submission problem)
SDN学习之Opendaylight浅析(四)
RT-Thread Studio 对代码进行格式化
DevExpress WPF入门指南 - 运行时生成的POCO视图模型(一)
GlobalMapper20 如何批量把dwg文件转换为dxf文件,效率神器
Rust异步框架的性能评估
Domestic mobile phone brands have found that they are nothing without the support of domestic consumers
remix连接测试链,连接Arbiturm二层网络
蓝桥杯练习012