public class MyPi {
public static void main(String[] args) {
// TODO Auto-generated method stub
int max = 1000;
double item = 0;
double sum = 0;
int flag = -1;
for (int n = 0; n <= max; n++) {
flag *= -1;
item = flag * 1.0 / (2 * n + 1);
sum += item;
}
System.out.println("π的近似值=" + sum * 4);
}
}
import java.io.*;
public class Pi
{
public static void main(String[] args)
{
final int max=1000;
double item=0;
double sum=0;
int flag=-1;
int n=0;
for(n=0;n<=max;n++)
{
flag*=-1;
item=flag*1.0/(2*n+1);
sum += item;
}
System.out.println(4*sum);
}
}
终于申诉成功~~~
void main()
{
int max = 1000;
double item=0, sum=0;
int flag = -1;
int n;
for(n=0;n<=max;n++)
{
flag*=-1;
item=flag*1.0/(2*n+1);
sum+=item;
}
printf("π=%f",4*sum);
}
long cut = 1000000; // 切圆次数
double PI = 0;
double a = 1 - 1.0 / 3;
double b = 5;
PI = a;
for (long i = 0; i < cut; i++) {
if (i % 2 == 0) {
PI += 1.0 / b;
} else {
PI -= 1.0 / b;
}
b += 2;
}
System.out.println(PI * 4);
以下是页面版的,打开谷歌浏览器,在粘贴到控制台按回车键即可。
var cut = 1000000; // 切圆次数
var PI = 0;
var a = 1 - 1.0/3;
var b = 5;
PI = a;
for (var i = 0; i < cut; i++) {
if(i % 2 ==0){
PI += 1.0 / b;
}else{
PI -= 1.0 / b;
}
b += 2;
}
console.log(PI * 4);
以上准确说是切圆的代码。
望采纳。
n < max;不应该是小于等于吧