实验二.cpp

来自「随机采样」· C++ 代码 · 共 52 行

CPP
52
字号
// 实验二.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 + =
减小字号Ctrl + -
显示快捷键?