两道c++语言题目跪求答案啊,大神们,急急----
发布网友
发布时间:2023-11-19 02:39
我来回答
共1个回答
热心网友
时间:2024-11-02 20:24
输出:
pi is 3.1415926536
n is 45.
#include <stdio.h>
#include <math.h>
double arctan(double input)
{
double b = 1,c,result = 0;
for(int i=1;;i+=2)
{
c = pow(input,i)/(double)i;
if(c < 0.000000000000001 )
break;
result += b*c;
b *= -1;
}
return result;
}
double computePI()
{
return 16.0*arctan(1.0/5.0)-4*arctan(1.0/239.0);
}
int SelectKFromN(int n,int k)
{
if(n == k || k== 0)
return 1;
return SelectKFromN(n-1,k)+SelectKFromN(n-1,k-1);
}
int main()
{
double pi = computePI();
printf("pi is %.10f\n",pi);
int n = SelectKFromN(10,2);
printf("n is %d.\n",n);
return 0;
}
追问亲,还有第二道题,一快做了吧
追答做了,在程序的 SelectKFromN 中啊,计算了10个选2个的结果是45