作者:sprout--_557 | 来源:互联网 | 2023-08-09 02:37
I am solving problem 9 on the Project Euler. In my solution I use a "goto" statement to break out of two for loops. The Problem is the following:
我正在解决Project Euler上的问题9。在我的解决方案中,我使用“goto”语句来打破两个for循环。问题如下:
A Pythagorean triplet is a set of three natural numbers, a b c, for which,
毕达哥拉斯三元组是一组三个自然数,一个b c,对此,
a^2 + b^2 = c^2
a ^ 2 + b ^ 2 = c ^ 2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 52.
例如,3 ^ 2 + 4 ^ 2 = 9 + 16 = 25 = 52。
There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
恰好存在一个毕达哥拉斯三元组,其中a + b + c = 1000.找到产品abc。
My solution is in c++:
我的解决方案是在c ++中:
int a,b,c;
const int sum = 1000;
int result = -1;
for (a = 1; a
Since "goto" statements are not very popular among c++ programmers, i would like to know, if this could be considered a reasonable use of "goto". Or if there is a better solution for the problem that doesn't need "goto". By that I don't mean a solution which just avoids "goto", but which avoids "goto" in a way that improves the algorithm.
由于“goto”语句在c ++程序员中不是很受欢迎,我想知道,如果这可以被认为是合理使用“goto”。或者,如果有一个更好的解决方案,不需要“转到”的问题。我并不是指一种只避免“转到”的解决方案,而是以改进算法的方式避免“转到”。
7 个解决方案