从1....n中随机输出m个不重复的数

从0到n-1中随机等概率输出m个不重复的数 题目描述假设n远大于m,编程实现从0到n-1中随机等概率的输出m个不重复的数。void knuth(int n,int m) { srand((unsigned int)time(0)); for(int i = 0;i<n;i++) { if(rand()%(n-i)<m) { cout<<i<<endl; m--; } } }思路解 阅读详情
void knuth(int n, int m) {
	srand((unsigned) time( NULL));
	for (int i = 0; i < n && m; i++) {
		//满足限定条件:rand() % (n - i) < m,即输出i,i递增不重复
		if (rand() % (n - i) < m) {
			cout << i << "\t";
			m--;
		} // end if
	} //end for
	return;
}

NOIP2006明明的随机数 描述 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。 输入 有2行,第1行为1个正整数,表示所生成的随机数的个数:N; 第2行有N个用空格隔开的正整数,为所产生的随机数。 输出 也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第 阅读详情

相关推荐

添柴 明明的随机数(桶排序+去重)

明明的随机数题目正确代码 题目 明明的随机数 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。 输入格式: 输入有2行 第1行为1个正整数,表示所生成的随机数的个数:N 第2行有N个用空格隔开的正整数,为所产生的随机数。 输出格式: 输出也是2行,第

weixin_52138829的博客 526

从1-n个数字中,随机筛选m个不重复的数字

public static int[] rand(int n, int m) { int num[] = new int[m]; while (true) { int random = (int) (Math.random() * n + 1);//可以直接后面乘100再加一。规律 for (int i = 0; i &lt; num.length...

司马缸砸光的博客 2038

深入解析Xilinx IDELAYE2在FPGA时序优化中的关键作用与实战配置

本文深入解析了Xilinx FPGA中IDELAYE2模块在时序优化中的关键作用。作为精密的信号延时微调器,IDELAYE2通过可编程的抽头延迟,有效解决高速接口中数据与时钟的相位偏差问题。文章详细阐述了其必须与IDELAYCTRL配对使用的核心机制,并对比分析了FIXED、VARIABLE和VAR_LOAD三种工作模式的实战配置方法与代码示例,为FPGA工程师提供了解决时序收敛难题的关键硬件配置指南。

pear55的博客 1184

一道迅雷笔试题引发的思考?—— 不重复随机算法

csdn上看到的一帖子 http://topic.csdn.net/u/20120825/20/6171393d-15ea-4a50-ba30-78d1d24974e3.html,是关于一种不重复随机算法,可以计算0 ~ n中不重复的m个数。 #include #include using namespace std; void knuth(int n, int m) { sr

ani_di的专栏 1381

人机协作新纪元:AI会抢走IT人的饭碗吗?

产品经理用自然语言描述需求,AI将其转化为技术规格书(准确率已达89%)

820

STM32G474片内运放实战:从独立模式到PGA与ADC采样链路

在嵌入式控制系统中,运放是连接模拟信号与数字处理的关键环节。传统方案依赖外部分立运放搭建放大电路,存在成本高、PCB布局敏感、一致性差等痛点。STM32G474在MCU内部集成多路可编程运放(OPAMP),支持独立模式、跟随器、PGA以及比较器模式,配合内部ADC和DMA可构成完整模拟采集链路。通过片内互联,信号路径大幅缩短,抗干扰性和通道一致性显著提升,尤其适用于电机电流采样、数字电源等场景。本文以STM32G474为例,详解从独立模式到PGA增益配置,再到ADC与DMA联动调试的完整过程,并给出采样时间

weixin_29194255的博客 247

给定1到N的随机数函数,产生1到M的随机数

给定能随机生成整数 1 到 n 的函数,写出能随机生成整数 1 到 m 的函数(m > n)。 设 n = 5, m = 7 关键是让生成的 1 到 7 的数出现概率相同。调用 n 次给定函数,生成 n 个 1 到 5 之间的随机数,选取最大数所在位置,直到剩下最后一个。如: 初始的 7 个数 [1, 2, 3, 4, 5, 6, 7]。 (1)7 个 1 到 5 的随机数,如 [5,

songuo' s home 5197

2006NOIP普及组真题 4. 数列

从上述推理中,我们发现要输出的幂次和中的第 N 项(也就是幂次和中的序号N),对应的二进制位数和幂次和对应的二进制位数相同。当 k=3 时,k的幂次为1, 3, 9, 27, 81…,即第0位,第2位均为1,转成 k 进制(k=3)时。,即第2位,第3位均为1,转成 k 进制(k=3)时。转成 k 进制(k=3)时的幂次和为。,即第0位,第1位,第2位均为1,(即第0位,第1位,第2位均为1)N=12时,12的二进制是。(即第0位,第2位均为1)(即第2位,第3位均为1)结论:这道题就变成了。

weixin_40252159的博客 1514

从0到n-1中随机等概率输出m个不同的数

//假设输入的n远大于m void knuth(int n, int m) { for (int i = 0; i < n; i++) { if (rand() % (n - i)<m) { cout << i << endl; m-...

diereng5160的博客 514

基于 Cognex VisionPro 的工业级图像匹配技术全解析

通过本文的深度解析,开发者可以系统掌握 Cognex VisionPro 图像匹配技术的核心原理、开发流程、优化策略及工业级应用。实际项目中,需结合具体场景灵活调整参数,综合运用预处理、特征提取和匹配算法,构建稳定可靠的视觉系统。

品质生活,极致编程。 1594

TT电机、310电机、370电机在智能小车与机器人中的性能对比与选型指南

本文深入对比了TT电机、310电机和370电机在智能小车与机器人项目中的核心性能差异。文章从尺寸、扭矩、转速、电压电流等关键参数入手,结合不同应用场景(如入门小车、竞技机器人、机械臂),提供了清晰的选型指南和驱动控制建议,帮助开发者根据项目需求选择最合适的电机方案。

weixin_29065015的博客 411

从0....n-1中随机等概率的输出m个不重复的数

从0....n-1中随机等概率的输出m个不重复的数

秋の水 554

python轮胎缺陷检测_基于深度学习的轮胎缺陷无损检测与分类技术研究

基于深度学习的轮胎缺陷无损检测与分类技术研究【摘要】:随着汽车行业快速发展及汽车保有量的持续增长,大大增加了对轮胎的需求,而轮胎质量在很大程度上决定了轮胎的使用寿命,也是在行驶过程中对人们生命财产安全的保障,同时对轮胎进行在线质量检测也便于轮胎生产企业及时发现生产线中存在的不合格产品,以此来调整机械设备甚至轮胎的生产工艺,避免造成生产材料的浪费。因此,检测轮胎质量是轮胎生产过程中必不可缺少的一部分...

weixin_39689428的博客 1426

从0....n-1中随机等概率的输出m个不重复的数。这里我们假设n远大于m

下面的程序可以从0....n-1中随机等概率的输出m个不重复的数。这里我们假设n远大于m 12345678910knuth(int n, int m){     srand((unsigned int)time(0));     for (int i = 0; i &lt; n; i++) {         if (rand()%(n-i)&lt;m ) {             cout &...

zwz2011303359的博客 1206

【Unity】【2D】用连续图片生成动画Animation的简明步骤

本文介绍了Unity中使用2D连续图片制作2D动画Animation的简明步骤。

O青草O的博客 Unity和游戏制作知识分享 718

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

下面的程序可以从0....n-1中随机等概率的输出m个不重复的数。(假设n>>m)

问题:题目下面的程序可以从0....n-1中随机等概率的输出m个不重复的数。这里我们假设n远大于m。 括号里应填写什么? ....... knuth(int n, int m) {      srand((unsigned int)time(0));      for (int i = 0; i         if (

rs勿忘初心的博客 2844

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

[迅雷笔试题]从1....n中随机输出m个不重复的数

思想:先以m/n的概率判断第一个数出不出现;如果出现,那么以(m-1)/(n-1)的概率判断下一个数出不出现;若不出现,那么以m/n-1的概率判断下一个数出不出现,等于如果前面的数不出现的话加大后面的数的出现概率。 #include "iostream" #include "ctime" #include "cstdlib" using namespace std; void knu

xingdebocqyz的专栏 1037

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

C++,1....n中随机等概率的输出m个不重复的数(假设n远大于m)。

#include <stdlib.h>#include <time.h>knuth(int n, int m) { srand((unsigned int)time(0)); for (int i = 0; i < n; i++) { if (rand()%(n-i)<m ) { ...

weixin_30919235的博客 257

明明的随机数

题目:明明的随机数 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了 N个 1到 1000之间的随机整数( N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。 输入格式 输入有 2行 第 1行为 1个正整数,表示所生成的随机数的个数:N。 第 2 行有 N个用空格隔开的正整数,为所产生的随机数。 输出格式 输出 2 行 第

weixin_52346600的博客 170

随机等概率的输出m个不重复的数

今天看到一段代码,可以从0.....n-1中随机等概率的输出m个不重复的数(n远远大于m)。遂记录下来。 首先,产生随机数,不免要用到srand,rand函数。先简单介绍下两个函数。 1,void srand(unsigned int seed);    初始化随机数生成器,对于每一个不同的seed,每一次调用rand()将会产生不同的随机数序列。   特别的,当seed == 1时,生成...

zb872676223的专栏 433

10,随机等概率的输出m个不重复的数

今天看到一段代码,可以从0.....n-1中随机等概率的输出m个不重复的数(n远远大于m)。遂记录下来。 首先,产生随机数,不免要用到srand,rand函数。先简单介绍下两个函数。 1,void srand(unsigned int seed);   初始化随机数生成器,对于每一个不同的seed,每一次调用rand()将会产生不同的随机数序列。   特别的,当seed == 1时,生成...

weixin_30733003的博客 293

如何高效产生m个n范围内的不重复随机数

转自:https://blog.csdn.net/code_crash/article/details/4875601  如何产生不重复的随机数?最容易想到的方法,是逐个产生这些随机数,每产生一个,都跟前面的随机数比较,如果重复,就重新产生。这是个很笨的方法,且比较次数呈线性增长,越往后次数越多。其实这些比较是多余的,完全可以不进行比较,只要反过来,按顺序产生这些数,但随机产生它们的位置。例如下...

gochenguowei的博客 1639

如何高效产生m个n范围内的不重复随机数(m<=n)

http://blog.sina.com.cn/s/blog_57de62c00100ltak.html 如何高效产生m个n范围内的不重复随机数(m    如何产生不重复的随机数?最容易想到的方法,是逐个产生这些随机数,每产生一个,都跟前面的随机 数比较,如果重复,就重新产生。这是个很笨的方法,且比较次数呈线性增长,越往后次数越多。其实这些 比较是多余的,完全可以不进行比较,只要反

xfortius的专栏 1630
上一篇: 匈牙利算法,求最大匹配数即最小顶点覆盖
下一篇: 一个数组[1,2,3,4,6,8,9,4,8,11,18,19,100] 前半部分是是一个递增数组,后面一个还是递增数组,但整个数组不是递增数组,那么怎么最快的找出其中一个数?
Edwards_June
博客等级 码龄12年 12粉丝 182原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值