码迷,mamicode.com
首页 > 其他好文 > 详细

2020.1.3计导全面复习

时间:2020-01-03 19:21:52      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:spl   二分查找   ctime   随机   event   +=   const   lld   show   

//用宏定义替换文本 
#include<iostream>
#include<cstdio>
#define p(m,n) printf("Hi,"#m"!\n""...And "#n"~\n")
int main(){
    p(John,Amy);
}

 

//条件编译
#include<iostream>
#include<cstdio>
#ifdef WIN32
#define PLL "%I64d"
#else
#define PLL "%lld"
#endif
int main(){
    long long x;
    scanf("%lld",&x);
    printf(PLL"\n",x);
}

 

//用带参宏计算圆的面积和周长
#include<iostream>
#include<cstdio>
#define PI 3.1415926
#define C(r) (2*PI*(r))
#define S(r) (PI*(r)*(r))
int main(){
    double x=1.2;
    printf("%lf\n%lf\n",C(x),S(x));
    return 0;
}

 

//线性同余产生随机数
#include<iostream>
#include<cstdio>
#include<ctime>
#define maxn 100010
const int a=975323;
const int b=2333333;
const int mod=152487967;
using namespace std;
int X[maxn];
int main(){
    X[0]=time(0);
    for(int i=1;i<=10;i++){
        X[i]=(1LL*a*X[i-1]+b)%mod;
    }
    for(int i=1;i<=10;i++)
        printf("%d ",X[i]);
    return 0;
}

 

//冒泡排序+二分查找
#include<iostream>
#include<cstdio>
using namespace std;
int n,a[1000];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
        for(int j=1;j<=n-i;j++)
            if(a[j]>a[j+1])swap(a[j],a[j+1]);
    int l=1,r=n,ans=0;
    for(int i=1;i<=n;i++)printf("%d ",a[i]);puts("");
    int x;scanf("%d",&x);
    while(l<=r){
        int mid=(l+r)>>1;
        if(a[mid]>=x){
            r=mid-1;
            ans=mid;
        }
        else l=mid+1;
    }
    printf("%d\n",ans);
    return 0;
} 
//蒙特卡洛算法求积分
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cstdio>
using namespace std;
double l,r,up,down;
double M,N;
int main(){
    srand(time(0));rand();rand();
    double x,y;
    l=1;r=2;up=1;down=0;
    for(int i=1;i<=1000000;i++){
        x=1LL*rand()*rand()*rand()%10000000;
        y=1LL*rand()*rand()*rand()%10000000;
        x+=10000000;
        x=x/10000000.0;
        y=y/10000000.0;
        if(x>=1&&x<=2&&y>=0&&y<=1)M++;
        if(x>=1&&x<=2&&y>=0&&y<1.0/x)N++;
    }
    double ans=1.0*N/M;
    printf("%f\n",ans);
    return 0;
}
技术图片
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
using namespace std;
int main(){
    srand(time(0));rand();rand();
    double x,y,M=0,N=0;
    for(int i=1;i<=10000000;i++){
        x=1LL*rand()*rand()*rand()%10000000;
        y=1LL*rand()*rand()*rand()%10000000;
        x/=10000000.0;
        y/=10000000.0;
        if(x<=1&&x>=0&&y<=1&&y>=0){
            M++;
            if(x*x+y*y<=1)N++;
        }
    }
    double ans=N*4.0/M;
    printf("%f",ans);
    return 0;
}
蒙特卡洛计算Π

 

2020.1.3计导全面复习

标签:spl   二分查找   ctime   随机   event   +=   const   lld   show   

原文地址:https://www.cnblogs.com/thmyl/p/12146270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!