碰到一个不会的有关于pi的
例如:
输入一个数字(5)
输出 3.1415
输入一个数字(6)
输出 3.14159
等等以此类推
10以内的就可以不用很大
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int s = 1;
double n = 1, t = 1, pi = 0;
while (fabs(t)>1e-7)
{
pi = pi + t;
n += 2;
s = -s;
t = s / n;
}
pi = pi * 4;
cout << "Π=" << setiosflags(ios::fixed) << setprecision(6) << pi << endl;
system("pause");
return 0;
}
这样是用公式来求的
也可以用C++库里自带的宏定义
在头文件cmath或math.h中有个宏定义M_PI可以直接用
有人可以解决一下吗