由于博主刚学到泊松分布,但当做题时需要查泊松分布表,奇怪的是比如λ=0.1,题目没有给相应数据,教材课后附表也没有给出,于是我萌生出自己编程实现泊松分布表的念头。 代码如下: 泊松分布公式:P{X=k}= C a b C_a^b Cab p k p^k pk ( 1 − p ) n − k (1-p)^{n-k} (1−p)n−k≈ λ k k ! \frac{λ^k}{k!} k!λk e − λ e^{-λ} e−λ
#include<bits/stdc++.h> using namespace std; long long f[100]; int main() { cout<<"The program is to calculate P{X=k} of Poisson distribution"<<endl; cout<<"The k is no more than 20\n"<<endl; f[0]=1; for(int i=1;i<=20;i++){ f[i]=f[i-1]*i; } double lambda,ans; int k; while(1){ cout<<"lambda="; cin>>lambda; cout<<"k="; cin>>k; ans=pow(lambda,k)*exp(-lambda)/f[k]; printf("P{X=%d}=%.7lf\n\n",k,ans); } return 0; }P{X≥x} =1-F(x-1)= ∑ r = x ∞ e − λ λ r r ! \sum_{r=x}^∞\frac{e^{-λ}λ^r}{r!} ∑r=x∞r!e−λλr
#include<bits/stdc++.h> using namespace std; long long f[100]; int k_recommend(double x){ if(0.1<=x&&x<=1.0) return 10; if(1.0<x&&x<3) return 15; if(3<=x&&x<=5) return 20; return 0; } int main() { cout<<"The program is to calculate P{X>=k} of Poisson distribution"<<endl; cout<<"The k is no more than 20"<<endl; cout<<"The lambda can only be 0.1<=lambda<=5.0"<<endl; cout<<"When you input a lambda, the program will give you the value of k automatically"<<endl; f[0]=1; for(int i=1;i<=20;i++){ f[i]=f[i-1]*i; } double lambda,p,ans[100]; int k,kk; while(1){ memset(ans,0,sizeof ans); cout<<"Input:\n"<<"lambda="; cin>>lambda;///input lambda if(k=k_recommend(lambda)){ kk=k; } else {cout<<"Your lambda are out of order, please input again!\n"<<endl; continue;} cout<<"k="<<kk<<endl; while(k--){ p=pow(lambda,k)*exp(-lambda)/f[k]; ans[k]=p+ans[k+1]; } cout<<"Output:"<<endl; for(int i=0;i<=kk-1;i++) printf("P{X>=%d}=%.7lf\n",i,ans[i]);///P{X>=k} =ans[k]; cout<<endl; } return 0; }以上代码可能会有许多不足之处,敬请各位大佬批评指正!