博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
360
阅读量:4452 次
发布时间:2019-06-07

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

#include <cstdio>

#include <cstring>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;

int sample(int * array, int N){

int sum=0;
for(int i=0;i<N;i++)
{
sum=sum+array[i];
}
vector<int> a;
// int k=0;
for(int i=0;i<N;i++)
{
for(int j=0;j<array[i];j++)
{
a.push_back(i);
// a[k++]=i;
}
}
int key = 1.0 * rand() / RAND_MAX * sum;
return a[key];
}
//array[3]={3,2,5}
//a[]={0,0,0,1,1,2,2,2,2,2}

int array[3] = {3,2,5};

int main()
{
// for (int i = 0; i < 1000000; i++){
// array[i] = rand();
//}
srand(time(NULL));
int t0=0,t1=0,t2=0;
for(int i=0;i<10000;i++)
{
int t=sample(array,3);
if(t==0)
t0++;
else if(t==1)
t1++;
else
t2++;
}
cout<<t0<<" "<<t1<<" "<<t2<<" "<<endl;
return 0;
}

转载于:https://www.cnblogs.com/dshn/p/9504919.html

你可能感兴趣的文章
好久没敲代码了,手有点生——一个小小的时钟
查看>>
运算符 AS和IS 的区别
查看>>
(转)详解C中volatile关键字
查看>>
easyui时的时间格式yyyy-MM-dd与yyyy-MM-ddd HH:mm:ss
查看>>
专题:动态内存分配----基础概念篇
查看>>
Codeforces Round #426 (Div. 2) (A B C)
查看>>
The Most Simple Introduction to Hypothesis Testing
查看>>
UVA10791
查看>>
P2664 树上游戏
查看>>
jQuery 停止动画
查看>>
Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
查看>>
MyBatis Generator去掉生成的注解
查看>>
教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构
查看>>
lua连续随机数
查看>>
checkstyle使用介绍
查看>>
history.js 一个无刷新就可改变浏览器栏地址的插件(不依赖jquery)
查看>>
会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!
查看>>
二维码图片生成
查看>>
在做操作系统实验的一些疑问
查看>>
Log4J日志配置详解
查看>>