计算圆周率
据说有个计算圆周率的外星人程序,如下:
#include<cstdio> #include<cstdlib> int a=10000,b,c=8400,d,e,f[8401],g; int main() { for(;b-c;) f[b++]=a/5; for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a) for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b); system("pause"); return 0; }
稍微整理:
#include<cstdio> #include<cstdlib> int main() { int f[8401], a, b, c = sizeof(f) / sizeof(f[0]) - 1, d, e, g; for (a = 10000, b = e = 0; b != c; ) f[b++] = a / 5; for (; g = c * 2; c -= 14, printf("%.4d", e + d / a), e = d % a) for (d = 0, b = c; d += f[b] * a, f[b] = d % --g, --b; d *= b) d /= g--; system("pause"); return 0; }
看不出怎么计算的。。/_\
计算圆周率的一个公式:
代码验证:
#include<cstdio> #include<cstdlib> int main() { double pi=2; for(int k=49;k>0;k--) pi=pi*k/(2*k+1)+2; printf("pi=%.15lf...\n",pi); system("pause"); return 0; }
输出结果:pi = 3.141592653589793...