该程序应该运行该函数,直到它满足条件(答案 < 0.01),然后报告该条件所需的服务器数量(c)。我的程序从来没有达到这一点,因为它在程序中途开始返回 nans。有人可以告诉我我做错了什么吗?
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
float product (float p);
float answer;
int main()
{
cout << "Searching for minimum number of servers..." << endl;
float c;
float answer;
do
{for (c=1; c<220; c++)
{
answer = product(c);
cout << "when c is " << c << " answer is " << answer << endl;
}
}while (answer >= 0.01);
cout << "when c is " << c << " answer is " << product(c) << endl;
cout << "Minimum number of servers required is " << c << endl;
return 0;
}
float product (float p)
{
float temp;
float result;
if (p==0)
answer = 1;
else
temp=200*product(p-1);
result=temp/(temp+p);
return result;
}