zoukankan      html  css  js  c++  java
  • 计算圆周率

    #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;
    }
  • 相关阅读:
    Rightmost Digit(快速幂+数学知识OR位运算) 分类: 数学 2015-07-03 14:56 4人阅读 评论(0) 收藏
    Can you find it? 分类: 二分查找 2015-06-10 19:55 5人阅读 评论(0) 收藏
    PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏
    HDU 1796 Howmany integers can you find (容斥原理)
    Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏
    POJ-1088 Skiing(记忆化搜索)
    悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 分类: dp 2015-05-21 10:50 14人阅读 评论(0) 收藏
    hdu 1010 Tempter of the Bone 深搜+剪枝
    索引原理与慢查询优化
    多表查询
  • 原文地址:https://www.cnblogs.com/WWHHTT/p/10145314.html
Copyright © 2011-2022 走看看