输出PI的近似值,为什么一直输出同一个值4

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double pi = 0;
    long i;
    long n;
    cout << "Enter the value of n: ";
    cin >> n;
    cout << endl;
    for (i = 0; i < n; i++)
    {
        if (i % 2 == 0||i == 0)
        {
            pi = pi + (1 / (2 * i + 1));
        }
        else
        {
            pi = pi - (1 / (2 * i + 1));
        }
    }
    pi = 4 * pi;
    cout << endl << "pi = " << pi << endl;
    return 0;
}

因为你的(1 / (2 * i + 1))是整数运算,当i=0是为1,之后一直是0;

pi = 4 * pi;

最后pi就是4