真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java近似求pi代碼,C語言近似求PI

java運(yùn)算中,如何引入π進(jìn)行計(jì)算(希望能附上例子)?

Java中Math.PI就表示π。其源碼為public static final double PI = 3.14159265358979323846。

成都創(chuàng)新互聯(lián)公司電話聯(lián)系:18982081108,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),成都創(chuàng)新互聯(lián)公司網(wǎng)頁制作領(lǐng)域十載,包括成都石雕等多個(gè)行業(yè)擁有豐富的網(wǎng)站推廣經(jīng)驗(yàn),選擇成都創(chuàng)新互聯(lián)公司,為企業(yè)錦上添花!

比如說你要算半徑為2的圓的面積,代碼如下:

double pi=Math.PI

int r=2

double S=pi*r*r

運(yùn)行結(jié)果如下圖

擴(kuò)展資料

package 計(jì)算π的近似值;

import java.util.Scanner;

public class Example1 {

public static void main(String[] args) {

Scanner scan=new Scanner(System.in);

System.out.println("請(qǐng)輸入精度:");

double z=scan.nextDouble();

System.out.println("在精度為"+z+"的條件下,π約等于:\n"+jishuPI(z));

}

static double jishuPI(double z){

double sum=2;

int n=1;

int m=3;

double t=2;

while(tz){

t=t*n/m;

sum=sum+t;

n++;

m+=2;

}

return sum;

}

}

參考資料:Java官網(wǎng) API-Class Math

Java編程求π問題

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

getPi(10000);

getPi(20000);

}

//輸入迭代次數(shù)

static void getPi(int n) {

float pi = 0;

for(int i = 1;i=n;i++) {

if(i%2==1) {

pi = pi + (float)1/(2*i-1);

}else {

pi = pi - (float)1/(2*i-1);

}

}

System.out.println("n="+n+";π="+4*pi);

}

}

編寫程序:計(jì)算π的近似值,π的計(jì)算公式為

#includestdio.h

main()

{ int n,i; double t,

sum;/*1*/

printf("請(qǐng)輸入n的值\n");

scanf("%d",n);

sum=2; i=1; t=2;/*2*/

while(in) { t=t*(2*i)*(2*i)/(2*i-1)/(2*i+1);

/*3*/ // sum=sum*t; i=i+1; }

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

或。

寫一個(gè)Java程序來實(shí)現(xiàn)蒙特卡洛法求π的近似值:

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class MonteCarloPi {

public static void main(String[] args) throws Exception{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("How many darts shoud I toss at the board?\n");

String s = br.readLine();

int numberOfDarts = Integer.parseInt(s.trim());

double radius = 1.0;

Dartboard d = new Dartboard(radius);

for(int i=1; i=numberOfDarts; i++){

Toss t = Toss.getRandom(radius);

d.strike(t);

}

double fractionIn = d.getFractionIn();

double pi = 4.0 * fractionIn;

System.out.println("Pi is approximately "+pi);

}

}

class Dartboard{

private double radius;

private int insideCircle, outsideCircle;

public Dartboard(double radius){

this.radius = radius;

insideCircle = 0;

outsideCircle = 0;

}

public void strike(Toss toss){

double x = toss.getX();

double y = toss.getY();

if(Math.sqrt(x*x + y*y) radius)

insideCircle++;

else

outsideCircle++;

}

public double getFractionIn() {

double total = (double) (insideCircle + outsideCircle);

return (double) insideCircle/total;

}

}

class Toss{

private double x,y;

public Toss(double x, double y){

this.x = x;

this.y = y;

}

public double getX(){return x;}

public double getY(){return y;}

public static Toss getRandom(double radius){

double x,y;

double size = Math.random();

double sign = Math.random();

size = size * radius;

if(sign 0.5)

x = size;

else

x = -size;

size = Math.random();

sign = Math.random();

size = size * radius;

if(sign 0.5)

y = size;

else

y = -size;

return new Toss(x,y);

}

}

擴(kuò)展資料:

C語言:用循環(huán)結(jié)構(gòu)分別編寫程序

#include

void main()

{

\x09int n=1;

\x09float temp;

\x09float sum=0;

\x09do

\x09{

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

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

\x09\x09\x09sum+=temp;

\x09\x09else

\x09\x09\x09sum-=temp;

\x09\x09n++;

\x09}while(temp=0.000001);

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

}


分享文章:java近似求pi代碼,C語言近似求PI
標(biāo)題來源:http://weahome.cn/article/hddosj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部