我是初学者 C++ 编码器(也是一般编程的初学者),我似乎遇到了问题。我的教授分配给我这个项目,我似乎大部分都做对了,但我一直得到一个错误的答案。对于我的程序,当它应该是40时,我一直得到42作为最终答案( b输入1.00,l输入2.00 ,p 输入0.01)。我将不胜感激任何帮助,并提前感谢任何建议或提示。
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
double b, l, p, num, P;
num = P = 0;
cout << "Enter the net profit, net loss, and the probabilty ";
cout << "of the products being sold respectively: ";
cin >> b >> l >> p;
if (p <= 0 || p >= 1)
{
cout << "\n";
cout << "The probability is either too large or small. " << endl;
cout << "Please try again." << endl;
}
else
{
cout << "\n";
while( (b / (b + l)) > P)
{
P += (p * pow((1-p),num));
num++;
}
cout << "It is optimal to order ";
cout << num + 1 << " products. " << endl;
}
system("PAUSE");
return 0;
}