主元素问题--蒙特卡罗
2015-12-11 15:00
204 查看
#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; }
相关文章推荐
- Gitlab代码管理平台部署(CentOS6.7—gitlab7.2.12)
- Gitlab代码管理平台部署(CentOS6.7―gitlab7.2.12)
- 利用 SGA 共享池,避开 parse 阶段
- Android获取系统当前日期及将日期转换成对应的星期
- android轮询最佳实践service+AlarmManager+Thread
- jobtracker自身启动与FIFO 调度器的工作过程
- [ActionScript 3.0] AS3 时间格式化方法
- 如何使用ueditor
- 无法使用distinct的时候想想分组group by
- ImageView宽度填满屏幕,高度自适应
- IntelliJ IDEA 常用插件收集
- SpriteKit解坑系列(三):投影Label
- 性能测试分享:MYSQL死锁1
- 读Android群英传第五章
- 织梦CMS调用文章列表时,怎么显示短时间格式
- powershell 汉洛塔
- H5 缓存机制浅析 移动端 Web 加载性能优化
- 在 Android 5.1.1 执行 remount system failed 解决方法
- 将日期控件绑定指定的EditText中,自然显示给定的时间
- 机器学习