⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 实验二.cpp

📁 随机采样
💻 CPP
字号:
// 实验二.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "math.h"
#include "stdlib.h"
#include "time.h"
#include "iostream.h"

int a[10000];
bool s[10000];
int n;
int Randomi(int range)
{
//	srand((unsigned)time(NULL)); 
	int i; 
	i = rand()%range+1;
	return i;
}

void RandomSampling(int m){

	int k=0;
	while(k<m){
		int r=Randomi(n);
		if(s[r]==false){
			k++;
			a[k]=r;
			s[r]=true;
		}
	}
}
int main(int argc, char* argv[])
{
	int m;
	srand((unsigned)time(NULL));
	while(true){
	cout<<"please input the total number:";
	cin>>n;
	cout<<"please input the choice number:";
	cin>>m;
	for(int i=1;i<=n;i++)
		s[i]=false;
	RandomSampling(m);
	for(i=1;i<=m;i++)
		cout<<a[i]<<"  ";
	cout<<endl;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -