好的,所以我花了最后2个小时来处理这个问题,将代码调整了一百次,但我一无所获。没有错误也没有警告,但答案是错误的。这是我的代码:
#include <iostream>
using namespace std;
void main()
{
/***********Variable Declarations************/
double count = 1, totalValue = 0, it, x, z=1, powe = 1, y;
cout << "Iterations=";
cin >> it;
cout << "x=";
cin >> x;
/***************End User Input***************/
while (count <= it)
{
for (int i = 0; i < powe; i++) {
z *= (x - 1) / (x + 1);
}
y = (1 / powe)*z;
totalValue = totalValue + y;
powe = powe + 2;
count++;
}
cout << "The Result is:" << 2*totalValue << endl;
}
我知道这是逻辑问题(数学),但我似乎无法找到它。谢谢。
编辑:我们不允许使用任何其他库。
答案 0 :(得分:4)
您的方法效率低,实施错误。
这是错误的,因为计算data.currentTS < updatetime
次幂的内部循环被破坏了。每次在内循环之前,您需要将2N
重置为1.
为了计算该系列的z
成员,您不需要从一开始就计算相同旧数字的N
幂。您刚刚在上一步计算了该数字的2N
次幂。使用它。
答案 1 :(得分:1)
您忘了在z
的每次迭代中将while
设置为1:
#include <iostream>
using namespace std;
void main()
{
/***********Variable Declarations************/
double count = 1, totalValue = 0, it, x, z=1, powe = 1, y;
cout << "Iterations=";
cin >> it;
cout << "x=";
cin >> x;
/***************End User Input***************/
while (count <= it)
{
for (int i = 0; i < powe; i++) {
z *= (x - 1) / (x + 1);
}
y = (1 / powe)*z;
totalValue = totalValue + y;
powe = powe + 2;
count++;
z = 1; //Without this line you will have very high powers
}
cout << "The Result is:" << 2*totalValue << endl;
}
编辑:
您可以通过不必一直从头开始计算功率来优化您的方法:
#include <iostream>
using namespace std;
void main()
{
/***********Variable Declarations************/
double count = 1, totalValue = 0, it, x, z, powe = 1, y;
cout << "Iterations=";
cin >> it;
cout << "x=";
cin >> x;
z = (x + 1) / (x - 1); //We start from power -1, to make sure we get the right power in each iteration;
//Store step to not have to calculate it each time
double step = ((x - 1) * (x - 1)) / ((x + 1) * (x + 1));
/***************End User Input***************/
while (count <= it)
{
z * = step;
y = (1 / powe)*z;
totalValue = totalValue + y;
powe = powe + 2;
count++;
//We no longer need to set z to 1, as the previous value becomes useful
}
cout << "The Result is:" << 2*totalValue << endl;
}
答案 2 :(得分:0)
可以避免计算的幂的改进 [x-1)/(x + 1) 在一个内循环中 这是使用累加器的解决方案 使用我的编程语言VRCalc ++编写的...
// OK !!!
@func user_log_opt (x) static
{
ratio = (x - 1) / (x + 1),
accumul = ratio,
total = accumul,
power = 3,
n = 20,
@while (power < n) {
accumul *= ratio,
accumul *= ratio,
total += (1 / power) * accumul,
power += 2
},
2.0 * total
},
有关VRCalc ++的更多信息,用您喜欢的工具进行搜索 搜索引擎...
以下是上述解决方案的C ++版本 (还包括Exp(x)函数)...
// C ++
namespace VRAxSamples {
long double Exp (long double x)
{
long double result = 1.0;
long double power_of_x = 1.0;
long int fact_of_k = 1;
int n = 16;
int k = 0;
while (k < n) {
power_of_x *= x;
fact_of_k *= (k + 1);
result += (power_of_x / (long double) fact_of_k);
++k;
}
return result;
}
long double Ln (long double x)
{
long double ratio = (x - 1) / (x + 1);
long double accumul = ratio;
long double total = accumul;
int power = 3;
int n = 20;
while (power < n) {
accumul *= ratio;
accumul *= ratio;
total += (1 / (long double) power) * accumul;
power += 2;
}
return 2.0 * total;
}
} // namespace
这就是所有的人...