介绍蒙特卡诺近似的例子代码

#include<fstream>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<ctime>
#include<memory>
using std::ifstream;
using std::ofstream;
using std::string;
using std::cout;
using std::endl;
using std::ios;
using std::unique_ptr;
double randx()
{
return rand() % / (double);
} int main(void)
{
ifstream ifs("D:/tex.pbm");
string header;
uint32_t w, h, l;
ifs >> header;
ifs >> w >> h >> l;
cout << "w: " << w << " ,h: " << h << " ,l: " << l << endl;
ifs.ignore();
unsigned char* pixles = new unsigned char[w * h * ];
ifs.read((char*)pixles, w * h * ); int nsamples = ;
srand(time());
float avgr = , avgg = , avgb = ;
float sumr = , sumg = , sumb = ;
for (int i = ; i < nsamples; i++)
{
float x = randx() * w;
float y = randx() * h;
int n = ((int)(y * w) + int(x)) * ;
sumr += pixles[n];
sumg += pixles[n + ];
sumb += pixles[n + ];
}
sumr /= nsamples;
sumg /= nsamples;
sumb /= nsamples;
for (int i = ; i < h; i++)
{
for (int j = ; j < w; j++)
{
int n = (i * w + j) * ;
avgr += pixles[n];
avgg += pixles[n + ];
avgb += pixles[n + ];
}
}
avgr /= w * h;
avgg /= w * h;
avgb /= w * h; printf("Average: %0.2f %0.2f %0.2f \n", avgr, avgg, avgb);
printf("Aproximation: %0.2f %0.2f %0.2f \n", sumr, sumg, sumb);
delete pixles; system("pause");
return ;
}

蒙特卡诺近似与PBM的更多相关文章

  1. R语言与正态性检验

    1.Kolmogorov-Smirnov正态性检验 Kolmogorov-Smirnov是比较一个频率分布f(x)与理论分布g(x)或者两个观测值分布的检验方法,若两者间的差距很小,则推论该样本取自某 ...

  2. 蒙特罗卡π算法(C++语言描述)

    圆的面积计算公式为:S=π*r*r 将圆放到一个直角坐标系中,如图黄色部分的面积是S/4=(π*r*r)/4;如果我们将取一个单位圆,则S/4=π/4. 因为是单位圆,半径为1,所以图中红色正方形的面 ...

  3. WPF弹出带蒙板的消息框

    效果图 思路 拿到父级窗体的内容,放入一个容器里,再在容器里放入一个半透明层.将整个容器赋给父级窗体的内容. 关闭时反向操作. 代码 消息窗弹出时 /// <summary> /// 弹出 ...

  4. 设置 tableview 的背景颜色,总是有蒙层

    1.给tableview添加了背景图片后, cell 总是有一层蒙层蒙着,很阴暗. 2.实验以后才发现背景图片被放在了 cell 的后面.

  5. 长文丨papi、咪蒙、罗胖之后,内容创业的机会在哪儿

    一.内容的一年 app死了,内容永生! 2016年,创业圈画风突变,昨天还在激辩java和PHP谁更好的技术宅们.还在梦想着成为第二个乔布斯改变世界的产品狗们.还在忽悠着用O2O横扫传统行业的小老板们 ...

  6. Replication的犄角旮旯(六)-- 一个DDL引发的血案(上)(如何近似估算DDL操作进度)

    <Replication的犄角旮旯>系列导读 Replication的犄角旮旯(一)--变更订阅端表名的应用场景 Replication的犄角旮旯(二)--寻找订阅端丢失的记录 Repli ...

  7. 蒙特·卡罗算法的Python实现

    一 背景 此算法诞生的背景是: 曼哈顿计划,有极大的计算需求. 计算机刚开始发展,最适合做计算. 蒙特卡洛算法理论基础是概率论,实际就是暴力计算逼近理想结果.正是在以上两个背景下,它刚好得到了极大的应 ...

  8. PRML读书会第十章 Approximate Inference(近似推断,变分推断,KL散度,平均场, Mean Field )

    主讲人 戴玮 (新浪微博: @戴玮_CASIA) Wilbur_中博(1954123) 20:02:04 我们在前面看到,概率推断的核心任务就是计算某分布下的某个函数的期望.或者计算边缘概率分布.条件 ...

  9. CSU 1328: 近似回文词

    省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1 ...

随机推荐

  1. How to proof MD5

    F(X, Y, Z) = (X&Y) | ((~X) & Z) G(X, Y, Z) = (X&Z) | (Y & (~Z)) H(X, Y, Z) = X^Y^Z I ...

  2. Converting PDF to Text in C#

    Parsing PDF files in .NET using PDFBox and IKVM.NET (managed code). Download source files - 82 kB [c ...

  3. [十二省联考2019]字符串问题——后缀自动机+parent树优化建图+拓扑序DP+倍增

    题目链接: [十二省联考2019]字符串问题 首先考虑最暴力的做法就是对于每个$B$串存一下它是哪些$A$串的前缀,然后按每组支配关系连边,做一遍拓扑序DP即可. 但即使忽略判断前缀的时间,光是连边的 ...

  4. centos7关闭ipv6

    方法一: 编辑文件 /etc/sysctl.conf ,新增以下两行 net.ipv6.conf.all.disable_ipv6 = net.ipv6.conf.default.disable_ip ...

  5. Zookeeper分布式集群原理与功能

    Zookeeper功能简介 ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现. 分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/订阅 ...

  6. Input标签使用整理

    0 写在前面 对于程序而言I/O是一个程序的重要组成部分.程序的输入.输出接口,指定了程序与用户之间的交互方式.对于前端开发而言,input标签也有着其重要地位,它为用户向服务端提交数据提供了可能. ...

  7. 交互题[CF1103B Game with modulo、CF1019B The hat、CF896B Ithea Plays With Chtholly]

    交互题就是程序与电脑代码的交互. 比如没有主函数的程序,而spj则给你一段主函,就变成了一个整体函数. 还有一种就是程序和spj之间有互动,这个用到fflush(stdout);这个函数就可以实现交互 ...

  8. gcc __thread关键字

    https://blog.csdn.net/xj178926426/article/details/54345449 EventLoop.cpp __thread EventLoop* t_loopI ...

  9. 解决js复制在安卓和ios兼容问题

    var clipboard = new ClipboardJS('.fr', { // target: function() { // return document.querySelector('d ...

  10. C++回顾day03---<异常>

    一:传统错误处理机制(C中通过函数返回来处理) int CalcRes(int n, int m, char ch, int& res) { ; switch (ch) { case '+': ...