计算圆周率
#include<iostream> #include<ctime> #include<algorithm> #include<cstdio> #include<cmath> using namespace std; inline int rd(){ int x=0,f=1; char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return x*f; } int main(){ srand(time(0)); system("color 9F"); unsigned long long cnt1=0,cnt2=0; unsigned long long n=rd(); if(n==0){ while(1){ for(int i=1;i<=1000000;i++){ long double x=(long double)((long double)rand()/(long double)(RAND_MAX)); long double y=(long double)((long double)rand()/(long double)(RAND_MAX)); long double z=(long double)(sqrt((long double)(x*x+y*y))); if(z<(long double)(1.0)) cnt1++; else if(z>(long double)(1.0)) cnt2++; } long double ans=(((long double)(cnt1+0.0))/((long double)(cnt1+cnt2+0.0))*(long double)(4.0)); printf("当前π的值为:%.10Lf",ans);puts(""); } } else{ for(int i=1;i<=n;i++){ long double x=(long double)((long double)rand()/(long double)(RAND_MAX)); long double y=(long double)((long double)rand()/(long double)(RAND_MAX)); long double z=(long double)(sqrt((long double)(x*x+y*y))); if(z<(long double)(1.0)) cnt1++; else if(z>(long double)(1.0)) cnt2++; } long double ans=(((long double)(cnt1+0.0))/((long double)(cnt1+cnt2+0.0))*(long double)(4.0)); printf("%.10Lf",ans); } return 0; }
升级版:
#include<iostream> #include<ctime> #include<algorithm> #include<cstdio> #include<cmath> using namespace std; inline int rd(){ int x=0,f=1; char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return x*f; } int main(){ srand(time(0)); system("color 9F"); unsigned long long cnt1=0,cnt2=0; unsigned long long n=rd(); unsigned long long cnt3=0; long double s=0; if(n==-1){ while(1){ cnt3++; cnt1=0,cnt2=0; for(int i=1;i<=1000000;i++){ long double x=(long double)((long double)rand()/(long double)(RAND_MAX)); long double y=(long double)((long double)rand()/(long double)(RAND_MAX)); long double z=(long double)(sqrt((long double)(x*x+y*y))); if(z<=(long double)(1.0)) cnt1++; else if(z>(long double)(1.0)) cnt2++; } long double ans=(((long double)(cnt1+0.0))/((long double)(cnt1+cnt2+0.0))*(long double)(4.0)); s+=ans; printf("\\(^-^)/ 当前π的值为:%.10Lf",s/(long double)(cnt3+0.0));puts(""); } } else if(n==0){ while(1){ for(int i=1;i<=1000000;i++){ long double x=(long double)((long double)rand()/(long double)(RAND_MAX)); long double y=(long double)((long double)rand()/(long double)(RAND_MAX)); long double z=(long double)(sqrt((long double)(x*x+y*y))); if(z<=(long double)(1.0)) cnt1++; else if(z>(long double)(1.0)) cnt2++; } cnt2-=22; long double ans=(((long double)(cnt1+0.0))/((long double)(cnt1+cnt2+0.0))*(long double)(4.0)); printf("\\(^-^)/ 当前π的值为:%.10Lf",ans);puts(""); } } else{ for(int i=1;i<=n;i++){ long double x=(long double)((long double)rand()/(long double)(RAND_MAX)); long double y=(long double)((long double)rand()/(long double)(RAND_MAX)); long double z=(long double)(sqrt((long double)(x*x+y*y))); if(z<(long double)(1.0)) cnt1++; else if(z>(long double)(1.0)) cnt2++; } long double ans=(((long double)(cnt1+0.0))/((long double)(cnt1+cnt2+0.0))*(long double)(4.0)); printf("%.10Lf",ans); } system("pause"); return 0; }
蒟蒻总是更懂你✿✿ヽ(°▽°)ノ✿