當前位置:編程學習大全網 - 源碼下載 - 掌握全局公式源代碼

掌握全局公式源代碼

# include & ltstdio.h & gt?

main()?

{ int n,I;雙t,

總和;/*1*/?

Printf("請為n \n輸入壹個值");?

scanf("%d ",& ampn);?

sum = 2;I = 1;t = 2;/*2*/?

while(我& ltn){ t = t *(2 * I)*(2 * I)/(2 * I-1)/(2 * I+1);

/* 3 *///sum = sum * t;I = I+1;}?

printf("π的值=%f\n ",t);/*4*/ }

或者。

寫壹個Java程序,用蒙特卡羅方法實現π的近似值;

導入Java . io . buffered reader;

導入Java . io . inputstreamreader;

蒙特卡洛皮公立學校{

公共靜態void main(String[] args)引發異常{

buffered reader br = new buffered reader(new InputStreamReader(system . in));

System.out.print("我應該向棋盤扔幾個飛鏢?\ n ");

string s = br . readline();

int number of darts = integer . parse int(s . trim());

雙倍半徑= 1.0;

鏢靶d =新鏢靶(半徑);

for(int I = 1;我& lt= numberOfDartsi++){

toss t = toss . get random(radius);

罷工;

}

double fraction in = d . getfraction in();

雙圓周率= 4.0 *分數;

System.out.println("Pi約為"+Pi);

}

}

等級鏢靶{

私人雙半徑;

private int insideCircle,outsideCircle

公共鏢靶(雙半徑){

this.radius = radius

inside circle = 0;

outside circle = 0;

}

公共無效罷工(扔擲){

double x = toss . getx();

double y = toss . gety();

if(math . sqrt(x * x+y * y)& lt;半徑)

inside circle++;

其他

outside circle++;

}

public double getFractionIn() {

double total =(double)(inside circle+outside circle);

return(double)inside circle/total;

}

}

扔硬幣

私有雙x,y;

公開投擲(雙x,雙y){

this.x = x

this.y = y

}

public double getX(){ return x;}

public double getY(){ return y;}

公共靜態Toss getRandom(雙半徑){

雙x,y;

double size = math . random();

雙符號= math . random();

尺寸=大小*半徑;

if(符號& gt0.5)

x =大小;

其他

x =-大小;

size = math . random();

sign = math . random();

尺寸=大小*半徑;

if(符號& gt0.5)

y =大小;

其他

y =-大小;

返回新的Toss(x,y);

}

}

擴展數據:

c語言:用循環結構單獨寫程序。

#包括

void main()

{

\ x09int n = 1;

\流動溫度;

\ x09 float sum = 0;

\x09do

\x09{

\ x09 \ x09 temp =(float)1/(2 * n-1);

\x09\x09if(n%2==1)

\ x09 \ x09 \ x09 sum+= temp;

\ x09 \ x09否則

\ x09 \ x09 \ x09 sum-= temp;

\ x09 \ x09n++;

\ x09 } while(temp & gt;=0.000001);

\x09printf("Pi=%f\n ",sum * 4);

}

  • 上一篇:大學剛畢業,工作該進阿裏巴巴還是進華為?
  • 下一篇:質量發展規劃和推廣活動的重點
  • copyright 2024編程學習大全網