求圆周率

来源:互联网 发布:复制消息打开手机淘宝 编辑:程序博客网 时间:2024/05/17 07:14
时间:16.4.12
文件:app.cpp
问题描述:累加法求圆周率


#include <iostream>
using namespace std;
int main( )
{
    int n,sign;
    double total,f;
    n=1;
    total=0;
    sign=1;
    f=1; 
    while(f>1e-5)
    {
        total+=(sign*f);
        n+=2;
        f=1.0/n;
        sign*=-1; 
    }
    cout<<"total="<<4*total<<endl;
    return 0;
}
0 0
原创粉丝点击