在 C 中使用嵌套循环将 for() 循环转换为 do{}while
Converting for() loop to do{}while in C with nested loops
我正在尝试编写一个程序,在用户输入最小值和最大值(dmin 和 dmax)后找到所有锐角三角形的解。现在我认为我的程序只使用 for() 循环工作,但我需要将第一个 for() 循环更改为 do{}while 循环,这让我感到困惑。我无法弄清楚如何编写 do{}while 循环,因此它还包括这些嵌套的 for() 循环和 if 语句。我尝试过的一切要么告诉我 b 和 c 没有被使用,要么它只是运行并且不提供任何输出。这是我的 for() 循环代码。
double a = 0, b = 0, c = 0;
printf("Here are the acute triangle solutions the program found:\n");
for (c = dmin; c <= dmax, c++)
{
for (b = dmin; b <= dmax; b++)
{
for (a = dmin; a <= dmax; a++)
{
if (a * a + b * b - c == c * c ){ //
if (((a + b) > c) && ((b + c) > a) && ((b + a) > b))
{printf("(%lf %lf %lf)\n", a, b, c);}
//sum of two sides must be greater than the third
//and angle across from c must be <90 degrees
}
}
}
}
一个for (e1;e2;e3) something
循环可以转化为:
e1;
while (e2) {
something;
e3;
}
或:
e1;
if (e2) {
do {
something;
e3;
} while (e2);
}
在一段时间内,您只需在之前进行初始化并在之后进行检查。它确实有点类似于系统对for循环所做的。
以下代码:
for (c = dmin; c <= dmax, c++)
{
for (b = dmin; b <= dmax; b++)
{
for (a = dmin; a <= dmax; a++)
{
if (a * a + b * b - c == c * c ){ //
if (((a + b) > c) && ((b + c) > a) && ((c + a) > c))
{printf("(%lf %lf %lf)\n", a, b, c);}
//sum of two sides must be greater than the third
//and angle across from c must be <90 degrees
}
}
}
}
变为:
c = dmin;
if(c < dmax) { //Make sure not to run once if c is greater
do
{
for (b = dmin; b <= dmax; b++)
{
for (a = dmin; a <= dmax; a++)
{
if (a * a + b * b - c == c * c ){ //
if (((a + b) > c) && ((b + c) > a) && ((c + a) > b))
{printf("(%lf %lf %lf)\n", a, b, c);}
//sum of two sides must be greater than the third
//and angle across from c must be <90 degrees
}
}
}
} while( ++c <= dmax );
}
我正在尝试编写一个程序,在用户输入最小值和最大值(dmin 和 dmax)后找到所有锐角三角形的解。现在我认为我的程序只使用 for() 循环工作,但我需要将第一个 for() 循环更改为 do{}while 循环,这让我感到困惑。我无法弄清楚如何编写 do{}while 循环,因此它还包括这些嵌套的 for() 循环和 if 语句。我尝试过的一切要么告诉我 b 和 c 没有被使用,要么它只是运行并且不提供任何输出。这是我的 for() 循环代码。
double a = 0, b = 0, c = 0;
printf("Here are the acute triangle solutions the program found:\n");
for (c = dmin; c <= dmax, c++)
{
for (b = dmin; b <= dmax; b++)
{
for (a = dmin; a <= dmax; a++)
{
if (a * a + b * b - c == c * c ){ //
if (((a + b) > c) && ((b + c) > a) && ((b + a) > b))
{printf("(%lf %lf %lf)\n", a, b, c);}
//sum of two sides must be greater than the third
//and angle across from c must be <90 degrees
}
}
}
}
一个for (e1;e2;e3) something
循环可以转化为:
e1;
while (e2) {
something;
e3;
}
或:
e1;
if (e2) {
do {
something;
e3;
} while (e2);
}
在一段时间内,您只需在之前进行初始化并在之后进行检查。它确实有点类似于系统对for循环所做的。
以下代码:
for (c = dmin; c <= dmax, c++)
{
for (b = dmin; b <= dmax; b++)
{
for (a = dmin; a <= dmax; a++)
{
if (a * a + b * b - c == c * c ){ //
if (((a + b) > c) && ((b + c) > a) && ((c + a) > c))
{printf("(%lf %lf %lf)\n", a, b, c);}
//sum of two sides must be greater than the third
//and angle across from c must be <90 degrees
}
}
}
}
变为:
c = dmin;
if(c < dmax) { //Make sure not to run once if c is greater
do
{
for (b = dmin; b <= dmax; b++)
{
for (a = dmin; a <= dmax; a++)
{
if (a * a + b * b - c == c * c ){ //
if (((a + b) > c) && ((b + c) > a) && ((c + a) > b))
{printf("(%lf %lf %lf)\n", a, b, c);}
//sum of two sides must be greater than the third
//and angle across from c must be <90 degrees
}
}
}
} while( ++c <= dmax );
}