计数迭代
Counting iterations
所以这就是问题:确定在达到条件 (a * a + b * b) > 4 之前需要以下操作的迭代次数:
newA = a * a - b * b + x
newB = 2 * a * b + y
a = newA
b = newB
我已经坚持了一段时间。任何帮助将不胜感激。
public static int findEscapeCount(double x, double y, int maxIterations)
{
int count =0;
double a;
double b;
double newA;
double newB;
for(a=0; a<maxIterations; a++)
{
for(b=0; b<maxIterations; b++)
{
newA = a * a - b * b + x;
newB = 2 * a * b + y;
a = newA;
b = newB;
if((a * a + b * b) < 4)
{
count = count+1;
}
}
}
return count;
}
如果我对问题的理解正确,那么这应该符合您的需要:
public static int findEscapeCount(double x, double y, int maxIterations)
{
int count =0;
double a;
double b;
double newA;
double newB;
while((a * a + b * b) > 4)
{
a++;
b++;
newA = a * a - b * b + x;
newB = 2 * a * b + y;
a = newA;
b = newB;
count++;
}
return count;
}
编辑: 重新阅读问题后,我什至不确定是否需要像您一样递增 "a" 和 "b" 每次迭代不管怎样,每次迭代都要重新分配它们……但你必须根据你的要求来确定!
所以这就是问题:确定在达到条件 (a * a + b * b) > 4 之前需要以下操作的迭代次数:
newA = a * a - b * b + x
newB = 2 * a * b + y
a = newA
b = newB
我已经坚持了一段时间。任何帮助将不胜感激。
public static int findEscapeCount(double x, double y, int maxIterations)
{
int count =0;
double a;
double b;
double newA;
double newB;
for(a=0; a<maxIterations; a++)
{
for(b=0; b<maxIterations; b++)
{
newA = a * a - b * b + x;
newB = 2 * a * b + y;
a = newA;
b = newB;
if((a * a + b * b) < 4)
{
count = count+1;
}
}
}
return count;
}
如果我对问题的理解正确,那么这应该符合您的需要:
public static int findEscapeCount(double x, double y, int maxIterations)
{
int count =0;
double a;
double b;
double newA;
double newB;
while((a * a + b * b) > 4)
{
a++;
b++;
newA = a * a - b * b + x;
newB = 2 * a * b + y;
a = newA;
b = newB;
count++;
}
return count;
}
编辑: 重新阅读问题后,我什至不确定是否需要像您一样递增 "a" 和 "b" 每次迭代不管怎样,每次迭代都要重新分配它们……但你必须根据你的要求来确定!