7-13 计算圆周率

输入格式:

输入在一行中给出小于1的阈值。

输出格式:

在一行中输出满足阈值条件的近似圆周率,输出到小数点后6位。

输入样例:

0.01

输出样例:

3.132157

ACCODE

#include 
#include 
using namespace std;

int main() {
    double threshold;
    cin >> threshold; // 输入阈值

    double numerator = 1.0;
    double denominator = 3.0;
    double term = 1.0;
    double sum = 1.0;

    int limit1 = numerator;
    int limit2 = denominator;
    while(term >= threshold){

        term = numerator/denominator;
        sum += term;
        limit1++;
        limit2+=2;
        numerator *= limit1;
        denominator *= limit2;

    }
    cout << fixed << setprecision(6) << 2 * sum << endl;
    return 0;

}

你可能感兴趣的:(PTA_c++,算法)