Calculate the value of π 幫忙看哪里錯 (15分)

2008-10-26 1:12 pm
Calculate the value of π from the infinite series

π=4-4/3+4/5-4/7+4/9-4/11.........

Print a table that shows the approximate value of π after each of the first 1000 terms of this series.





#include <iostream>

using namespace std;

int main()
{
long pi=4;
int n=999 ; //each of the first 1000 terms of this series
double s; //分母

for(int i=0;i<=n;i=i+2)
{
s=i+s;
pi = pi + 4/-s;
}

cout<<"pi的值為:"<<pi<<endl;

return 0;}

謝~

回答 (1)

2008-10-26 11:05 pm
✔ 最佳答案
Please refer to my solution to the problem..

#include <stdio.h>

int main(void)
{
double pi = 0.0;
int i, j, k;

for (i = 0, j = 1, k = 3; i < 1000; i+=2, j+=4, k+=4)
{
pi += 8.0/((double)j * (double)k);
printf_s("%f\n", (double)pi);
}

return 0;
}


收錄日期: 2021-04-23 12:43:23
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081026000051KK00317

檢視 Wayback Machine 備份