在 Java 中求解具有两个变量的困难方程

发布于 2024-12-26 04:00:53 字数 856 浏览 3 评论 0原文

我的作业要求执行以下操作:
Search2:搜索 x*x + y*y - 12x -10y + 36 = 0 的解。在 x 和 y 中从 0 到 10 搜索,在移动到下一个 x 之前搜索每个 y 值。打印找到的前三个解决方案。 (注意 - 带标签的中断在这里很方便!)

我无法弄清楚这一点的逻辑。我想我必须使用 2 个以上的循环,但不确定。
这就是我到目前为止所拥有的(它只是重复(6,0)):

for (int j = 0; j <= 10; j++) {
    for (int i = 0; i <= 10; i++) {
        while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
            System.out.println("(" + i + ", " + j + ")");  
        }  
    }  
}  

更新
这是解决方案:

    int t = 0;

    for (int i = 0; i <= 10; i++) {
        if (t == 3) {
            break;
        }
        for (int j = 0; j <= 10; j++) {
            if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
                System.out.println("(" + i + ", " + j + ")");
                t++;
            }
        }
    }

My assignment says to do the following:
Search2: Search for a solution to x*x + y*y - 12x -10y + 36 = 0. Search from 0 to 10 in both x and y, searching every y value before moving to the next x. Print the first three solutions found. (Note - a labelled break is handy here!)

I can't figure out the logic for this. I think I have to use more than 2 loops but not sure.
This is what I have so far (It just repeats (6,0)):

for (int j = 0; j <= 10; j++) {
    for (int i = 0; i <= 10; i++) {
        while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
            System.out.println("(" + i + ", " + j + ")");  
        }  
    }  
}  

UPDATE
Here is the solution:

    int t = 0;

    for (int i = 0; i <= 10; i++) {
        if (t == 3) {
            break;
        }
        for (int j = 0; j <= 10; j++) {
            if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
                System.out.println("(" + i + ", " + j + ")");
                t++;
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

暗恋未遂 2025-01-02 04:00:53

不错的尝试。因为你已经很接近了,所以我会向你展示一个可行的解决方案。基本上,您需要做三件事:

  1. while 更改为 if
  2. 使用变量来计算找到解决方案的次数,这样您就可以在三个处停止
  3. 添加标签这样您就可以从内部循环中跳出外部循环,

为了清楚起见,我还建议您使用与问题相同的变量名称 - 即 xy

int count = 0;
outerLoop:
for (int y = 0; y <= 10; y++) {
    for (int x = 0; x <= 10; x++) {
        if (x * x + y * y - 12 * x - 10 * y + 36 == 0) {
            System.out.println("(" + x + ", " + y + ")");  
            if (++count == 3)
                break outerLoop;
        }
    }
}

执行时,此代码会生成:

(6, 0)
(3, 1)
(9, 1)

抱歉给您喂食,但这里的课程的一部分是良好的编码风格和实践。

Not a bad attempt. Because you're so close, I'll show you a working solution. Basically, you need to do three things:

  1. Change while to if
  2. Use a variable to count the number of times you find the solution, so you can stop at three
  3. Add a label so you can break out of the outer loop from within the inner loop

I also recommend you use variable names the same as the problem - ie x and y - for clarity.

int count = 0;
outerLoop:
for (int y = 0; y <= 10; y++) {
    for (int x = 0; x <= 10; x++) {
        if (x * x + y * y - 12 * x - 10 * y + 36 == 0) {
            System.out.println("(" + x + ", " + y + ")");  
            if (++count == 3)
                break outerLoop;
        }
    }
}

When executed, this code produces:

(6, 0)
(3, 1)
(9, 1)

Sorry for spoon-feeding you, but part of the lesson here is good coding style and practice.

作死小能手 2025-01-02 04:00:53

您正在使用一个无限期运行的额外 while 循环。

while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
    System.out.println("(" + i + ", " + j + ")");  
}

第一次计算结果为真时 - 即当它达到 (6,0) 时 - 它将继续运行,因为 ij 未在内部修改。

您需要将其替换为 if

You're using an extra while loop that runs indefinetly.

while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
    System.out.println("(" + i + ", " + j + ")");  
}

The first time this evaluates to true - i.e. when it reaches (6,0) - it will keep running because i and j are not modified inside.

You need to replace it with an if.

温柔戏命师 2025-01-02 04:00:53

仔细看看你的内部 while 循环。一旦找到方程的解,ij 就永远不会改变,并且公式的计算结果始终为 0,从而导致无限循环。

为了清楚起见,将 ij 重命名为 x 和 y 也可能是明智的。不过,你基本上走在正确的轨道上。不要忘记您只需打印前三个解决方案。

Take a good look at the inner while loop you have. Once the solution to the equation is found, i and j never change, and the formula always evaluates to 0, resulting in an infinite loop.

It might also be wise to rename i and j to x and y, for clarity. You're mostly on the right track though. Don't forget you only have to print the first three solutions.

つ低調成傷 2025-01-02 04:00:53

我不会给你太多帮助,因为这是一个非常简单的概念,但想想你需要循环什么,它可能会更容易使用 x 和 y 代替 i 或 j。另外,您仅打印 (6,0),因为这正是您告诉它对 while 循环执行的操作。

一个提示,您的 while 循环应该有一个停止其功能的语句,假设 if x < 4、如果它大于或= to 则它将在循环外继续。

I wont help you much since this is a pretty simple concept, but think about what you need to loop through, it might make it easier to instead use x and y instad of i or j. Also your printing only (6,0) because thats exactly what you told it to do with the while loop.

One hint, your while loop should have a statement that stops its function, lets say if x < 4, if it ever is greater or = to than it will continue outside the loop.

若有似无的小暗淡 2025-01-02 04:00:53
public class EquationSolver {

    public static void main(String[] args) {

        int found = 0;
        searchSolutions:
        for (int y = 0; y <= 10; y++) {
            for (int x = 0; x <= 10; x++) {
                if (((x * x) + (y * y) - (12 * x) - (10 * y) + 36) == 0) {
                    System.out.println("(" + x + ", " + y + ")");
                    found ++;
                    if (found == 3) {
                        break searchSolutions;
                    }
                }
            }

        }

    }

}
public class EquationSolver {

    public static void main(String[] args) {

        int found = 0;
        searchSolutions:
        for (int y = 0; y <= 10; y++) {
            for (int x = 0; x <= 10; x++) {
                if (((x * x) + (y * y) - (12 * x) - (10 * y) + 36) == 0) {
                    System.out.println("(" + x + ", " + y + ")");
                    found ++;
                    if (found == 3) {
                        break searchSolutions;
                    }
                }
            }

        }

    }

}
你好,陌生人 2025-01-02 04:00:53

我上次写这类作业已经很久了……只是为了好玩:)

public class Main {

    public static void main(String[] args) {
        int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        boolean isSolutionFound = Boolean.FALSE;
        int solCounter = 0;

        searchSolutions:
        for (Integer y : range) {
            for (Integer x : range) {
                isSolutionFound = checkForSolution(x, y);
                if (isSolutionFound) {
                    printSolution(x, y);
                    solCounter++;
                }

                if (solCounter == 3) 
                    break searchSolutions;
            }
        }
    }

    private static void printSolution(Integer x, Integer y) {
        System.out.println(x + "," + y); // use some fancy formatting instead
    }

    private static boolean checkForSolution(int x, int y) {
        if (x * x + y * y - 12 * x - 10 * y + 36 == 0)
            return true;
        else
            return false;
    }

}

Long time I last wrote some homework of that sort...just for the fun of it :)

public class Main {

    public static void main(String[] args) {
        int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        boolean isSolutionFound = Boolean.FALSE;
        int solCounter = 0;

        searchSolutions:
        for (Integer y : range) {
            for (Integer x : range) {
                isSolutionFound = checkForSolution(x, y);
                if (isSolutionFound) {
                    printSolution(x, y);
                    solCounter++;
                }

                if (solCounter == 3) 
                    break searchSolutions;
            }
        }
    }

    private static void printSolution(Integer x, Integer y) {
        System.out.println(x + "," + y); // use some fancy formatting instead
    }

    private static boolean checkForSolution(int x, int y) {
        if (x * x + y * y - 12 * x - 10 * y + 36 == 0)
            return true;
        else
            return false;
    }

}
魂归处 2025-01-02 04:00:53
for (int j = 0; j <= 10; j++)
{
   for (int i = 0; i <= 10; i++)
   {
      if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0)
      {
         System.out.println("(" + i + ", " + j + ")");
         return;
      }  
   }  
} 

记住 return; 部分很重要,否则尽管您已经找到了解决方案,但您仍然会搜索解决方案。如果您不需要更多解决方案,那么您应该省略“return”语句。

for (int j = 0; j <= 10; j++)
{
   for (int i = 0; i <= 10; i++)
   {
      if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0)
      {
         System.out.println("(" + i + ", " + j + ")");
         return;
      }  
   }  
} 

It is import to remember the return; part as you would otherwise still search for solutions although you already found one. If you wan't more solutions, then you should should omit the ´return´ statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文