主元素问题--蒙特卡罗
来源:互联网 发布:虚拟机运行mac os x慢 编辑:程序博客网 时间:2024/04/30 10:14
#include "iostream"#include "ctime"#include "cmath"#include "random.h" //自己编写的头文件using namespace std;int key;template<class Type>bool majority(Type T[], int n) //判断主元素的蒙特卡洛方法{ int i; RandomNumber rnd; int index = rnd.Random(n); //产生0--n-1的下标 key = T[index]; int count = 0; for(i=0; i<n; i++) if(key == T[i]) count++; return count / n > 0.5;}//重复调用count次蒙特卡罗方法template<class Type>bool major(Type T[], int n, double e, double p) //可使算法的错误概率小于e, p为得到正确解的概率,0.5<p<1{ int count = (int)ceil( log(e) / log(p) ); for(int i=1; i<=count; i++) if(majority(T, n)) return true; return false;}int main() { int a[] = {5, 5, 5, 5, 5, 5, 1, 3, 4, 6}; if(major(a, 4, 0.01, 0.6)) cout << "存在主元素: " << key << endl; else cout << "不存在主元素!\n"; return 0;}
0 0
- 主元素问题--蒙特卡罗
- 蒙特卡罗算法--主元素问题
- 主元素问题
- 主元素问题
- 主元素问题
- 主元素问题
- 主元素问题
- 主元素问题
- 主元素问题求解
- 主元素问题
- 主元素问题
- 主元素问题
- 主元素问题
- 主元素问题
- 寻找主元素问题
- Lintcode主元素问题
- 寻找多数元素/主元素问题
- 【随机化算法】蒙特卡罗算法,主元素问题,素数测试问题
- Map遍历的几种方法
- 如何使用ueditor
- 将日期控件绑定指定的EditText中,自然显示给定的时间
- cron表达式详解
- Android获取系统当前日期及将日期转换成对应的星期
- 主元素问题--蒙特卡罗
- encodeURIComponent
- IIC协议的知识总结与积累
- 字符间距,行间距
- windows密钥查看器ProduKey1.82汉化
- 重写onCreateView方法创建Dialog
- IOS实现可以单独改变下划线颜色的UILabel
- 解决绕过android下apk使用usb设备权限查询相应问题,自动获取usb权限
- 数据预处理之特征标准化