博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编程珠玑: 12章 取样问题 12.1程序的输入包含两个整数m和n,其中m<n。输出是0~n-1范围内m个随机整数的有序列表,不允许重复-------解题总结
阅读量:3656 次
发布时间:2019-05-21

本文共 1027 字,大约阅读时间需要 3 分钟。

#include 
#include
#include
#include
using namespace std;/*问题:程序的输入包含两个整数m和n,其中m
max) { int temp = min; min = max; max = temp; } return ( rand() % (max - min + 1) + min );}//生成0~n-1中m个随机选择的不重复的数组成的有序列表vector
getRandomVector(int m , int n){ vector
results; //前面m个数的直接拷贝 for(int i = 0 ; i < m ; i++) { results.push_back(i); } //后面的随机替换 for(int i = m ; i < n ; i++) { int k = randRange(0 , n - 1); if(k <= m - 1) { results.at(k) = i; } } //替换后的结果注意要排序 sort(results.begin() , results.end()); return results;}void print(vector
& results){ if(results.empty()) { cout << "no result" << endl; return; } int size = results.size(); for(int i = 0 ; i < size; i++) { cout << results.at(i) << " "; } cout << endl;}void process(){ int m , n; vector
results; while(cin >> m >> n) { results = getRandomVector(m , n); print(results); }}int main(int argc , char* argv[]){ process(); getchar(); return 0;}

转载地址:http://uhofn.baihongyu.com/

你可能感兴趣的文章
JDBC技术(三)——预防SQL注入攻击
查看>>
JDBC技术(五)——JDBC工具类
查看>>
JDBC(六)——JDBC读取数据表,将数据存入对象中,并将对象存储到集合中
查看>>
hibernate原理
查看>>
IDEA快捷键
查看>>
Struts2原理
查看>>
activemq总结
查看>>
jdk环境变量配置
查看>>
mybatis原理
查看>>
spring原理
查看>>
AOP
查看>>
JDK和JRE的区别
查看>>
zookeper正式集群搭建(非伪集群)
查看>>
linux定时备份mysql(可用)
查看>>
linux使用链接下载文件
查看>>
maven配置阿里云仓库
查看>>
idea生成mybatis实体的方法
查看>>
idea逆向工程mybatis
查看>>
oracle纯url连接字符串
查看>>
oracle自动提交事务以及手动
查看>>