#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
long int i;
double x,y,m=0,n=0;
srand(time(NULL));
for(i=1;i<=100000000;i++)
{
x=((double)(rand()%10))/10+((double)(rand()%10))/100+((double)(rand()%10))/1000+((double)(rand()%10))/10000+((double)(rand()%10))/100000+((double)(rand()%10))/1000000+((double)(rand()%10))/10000000+((double)(rand()%10))/100000000+((double)(rand()%10))/1000000000;
y=((double)(rand()%10))/10+((double)(rand()%10))/100+((double)(rand()%10))/1000+((double)(rand()%10))/10000+((double)(rand()%10))/100000+((double)(rand()%10))/1000000+((double)(rand()%10))/10000000+((double)(rand()%10))/100000000+((double)(rand()%10))/1000000000;
if((x*x+y*y)<=1) m++;n++;
if(i<=100){
if((int)((double)(long int)m/n*4*1000000/1000000)*10000==31415) {printf("第%ld次射击\tπ=%.4lf\n",i,(double)(long int)m/n*4*1000000/1000000);goto k;}
else printf("第%ld次射击\tπ=%.4lf\n",i,(double)(long int)m/n*4*1000000/1000000);
}
else if((i<=10000)&&(((long int)i%100)==0)) {
if((int)((double)(long int)m/n*4*1000000/1000000)*10000==31415) {printf("第%ld次射击\tπ=%.4lf\n",i,(double)(long int)m/n*4*1000000/1000000);goto k;}
else printf("第%ld次射击\tπ=%.4lf\n",i,(double)(long int)m/n*4*1000000/1000000);
}
else if((i<=1000000)&&(((long int)i%10000)==0)){
if((int)((double)(long int)m/n*4*1000000/1000000)*10000==31415) {printf("第%ld次射击\tπ=%.4lf\n",i,(double)(long int)m/n*4*1000000/1000000);goto k;}
else printf("第%ld次射击\tπ=%.4lf\n",i,(double)(long int)m/n*4*1000000/1000000);
}
else if(i%1000000==0) {
if((int)((double)(long int)m/n*4*1000000/1000000)*10000==31415){ printf("第%ld次射击\tπ=%.4lf\n",i,(double)(long int)m/n*4*1000000/1000000);goto k;}
else printf("第%ld次射击\tπ=%.4lf\n",i,(double)(long int)m/n*4*1000000/1000000);
}
}
k: return 0;
}
怎么能算到3.1415就立即停止~~
要统计次数的 我也不想这么麻烦.......
用的是如图的公式,下面是arctg展开的级数。
这个输出没有写好,一组是10位,如果前面是“0”就会少了,要完善一下。
N是定义 算多少组的,N=1000 时 N*10就是10000组,要算到100万位这种算法要好长时间,但是这是最简单的算法之一。
下面分成三个部分,第一是定义数组和
#define N 1000
#include "math.h"
#include "string.h"
main()
{
auto double a1,b1,b2,c1,d,i=1.0,q=-1.0;
auto double n,g,r=10000000000.0;
static double a[N+1],b[N+1],c[N+1];
auto char *str3,str1[15],strs1[11*N],*p_strs1;
auto long p,j,k=1,m=1;
do {
printf("input p= ");
scanf("%ld",&p);
} while(p<=0 && p>100);
p = p + 1;
b[1]=r*20.0,c[1]=239*r;
do
{ q=-q,a1=0,b1=0,c1=0;
for(j=m;j<=p;j++){c1=c[j]+c1*r,c[j]=floor(c1/57121),c1=c1-c[j]*57121;}
if (c[m]<1e-5) m++;
for(j=k;j<=p;j++){
a1=b[j]+a1*r,b[j]=floor(a1/25.0),a1=a1-b[j]*25.0;
d=b1*r+b[j]-c[j],b1=floor(d/i),a[j]=4*q*b1+a[j];
b1=d-b1*i;
}
if (b[k]<=1e-5) {
k++ ;
printf("k=%ld ",k);}
i = i + 2;
}while(m<=p);
do
{ q = -q, a1 = 0,b1 = 0;
for(j=k;j<=p;j++) {
a1=b[j]+a1*r,b2=floor(a1/25),b[j]=b2;
a1=a1-b2*25,d=b2+b1*r;
b1=floor(d/i),a[j]=a[j]+4*q*b1;
b1=d-b1*i;
}
if (b[k]<=1e-5) { k++;printf(" k= %ld",k);}
i = i + 2;
}while(k<=p);
printf("\n");
for(j=p;j>=1;j--){g=a[j]+n,n=floor(g/r),a[j]=g-r*n;}
for(j=1;j<p;j++) printf("%10.0f ",a[j]);
str3=strs1;
}
c语言 圆周率~~ 大神们帮我看看哪里错了!
return 0;}
c语言求圆周长和面积
假设已知圆的半径为r,圆周长的计算公式为:周长 = 2πr;圆的面积计算公式为:面积 = πr²。根据这两个公式,可以编写如下的C语言程序来计算圆的周长和面积。c include define PI 3.14159 \/\/ 定义圆周率PI的值 int main { double radius, circumference, area; \/\/ 定义半径、周长...
能用C语言或C#语言算圆周率吗?
```c include <stdio.h> include <math.h> int main() { int n = 6; \/\/ 初始为六边形 double side = 1.0; \/\/ 正多边形的边长 double radius = 1.0; \/\/ 单位圆的半径 double pi = 0.0; \/\/ 圆周率的估计值 while (n <= 1048576) { \/\/ 增加多边形的边数直到达到 2^...
用C语言编写一个求圆周率的程序
楼上的那个 i<400这个400改成更大的数精确度会高点,还有printf后改成%.50lf\\n可以算到小数点后50位,这个50也是可以改的。外加一句,一般电脑很难算到50位
用C语言写出计算圆周率的程序
double s = 0;\/\/面积 double pi = 0;\/\/圆周率 double d=200;\/\/直径(值越大圆周率越精确)double r = d\/2;\/\/半径 for(int i=1;i<=d;i++){ for(int j=1;j<=d;j++){ if((i-r)*(i-r)+(j-r)*(j-r)<=r*r){ s++;} } } pi = s\/(r*r);...
C语言:计算圆周率(精度保留到小数点后6位),用程序实现,公式如下:_百...
include<stdio.h> include<math.h> int main(){double pi=1,t=0;for(;t<1.9999999;){t=sqrt(2+t);pi*=2\/t;} printf("%lf\\n",pi*2);return 0;}
我想找个C语言程序 能算圆周率的
for(;d=0,g=c*2;c-=14,printf("%04d",e+d\/a),e=d%a)for(b=c;d+=f[b]*a,f[b]=d%--g,d\/=g--,--b;d*=b);} 程序绝对正确,不过还没看懂。另一个 首先要知道圆周率的算法,用圆内正n边形去逼近圆 PI*r*r = 0.5n*r*r*sin(360\/n)PI = 0.5nsin(360\/n)for(...
用c语言编程求出圆的周长、和面积是多少。
程序代码如下:直接编译,程序输出结果中任意输入半径r,程序执行结果如下图所示:
c语言、编写程序求圆的周长和面积,要求将圆周率定义为符号常量。_百度...
include<stdio.h> define pi 3.1415926 void main(){ float r,l,s;printf("请输入圆的半径:");scanf("%f",&r);l=2*pi*r;s=pi*r*r;printf("圆的周长为:%f\\n,圆的面积为:%f\\n",l,s);}
C语言求圆周率
scanf("%f",&f)==1) { pi=0; i=1; sign=1; do { pi+=sign*1.0\/i; }while(1.0\/i>=f&&(sign=-sign)&&(i+=2)); printf("%lf\\n",pi*4.0); } return 0;}