#include<stdio.h> #include<math.h> #define p1 0.02 #define p2 0.98 int main() { double p; p=1-pow(p2,150)-150*p1*pow(p2,149); printf("P=%lf\n",p); getch(); return 0; } //用蒙特卡羅方法求概率,C語言編程 #include <chrono> #include <iostream> #include <random> using namespace std; int main() { unsigned seed = chrono::system_clock::now().time_since_epoch().count(); mt19937 g(seed); uniform_real_distribution<double> d(0.0, 1.0); const unsigned N = 10000; unsigned count = 0; for (unsigned i = 0; i != N; ++i) if (d(g) + d(g) + d(g) <1) ++count; cout << count << "/" << N << endl; return 0; }
C++
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.