简体   繁体   English

如何使用逻辑循环打印出20个毕达哥拉斯数的非全等三角形

[英]How can I use logic loop to print out 20 Pythagorean numbers of non-congruent triangles

How can I use logic loop to print out 20 Pythagorean numbers of non-congruent triangles. 如何使用逻辑循环打印出20个毕达哥拉斯数的非全等三角形。 Without repeating numbers ie if I have 4,3,5 I can't have 3,4,5. 没有重复数字,即如果我有4,3,5我不能有3,4,5。

I was using "for" loops but I don't know how to remove the repeating answers. 我正在使用“for”循环,但我不知道如何删除重复的答案。

for (k = 0; k < 50; k++)
    {
        for ( i = 0; i < 50; i++)
        {
            for ( j = 0; j < 50; j++)
            {
                if ( (k+1)*(k+1) + (i+1)*(i+1) == (j+1)*(j+1) )
                {
                    System.out.println( "\n\n\t\tThe numbers are : " + (k+1) + ", "
                                                                     + (i+1) + ", "
                                                                     + (j+1) );
                }
            }
        }
    }

Two (related) options come to mind: You could "keep" each of your triples in a sorted order, so when you find {4,3,5} you turn it into {3,4,5} before saving it for subsequent comparison with any others for uniqueness. 我想到了两个(相关的)选项:你可以按照排序的顺序“保留”你的每个三元组,所以当你找到{4,3,5}时,你将它变成{3,4,5},然后保存它与其他任何人比较的独特性。 Or, you could create a "Triple" class where you define a method like boolean equals(final Triple rval) that does the comparison of each element from lowest to highest. 或者,您可以创建一个“Triple”类,您可以在其中定义一个类似boolean equals(final Triple rval) ,该方法可以对每个元素进行从最低到最高的比较。 Of course, there are probably other ways this could be done as well. 当然,也有可能采取其他方式。

UPDATE: Given the code you just added, if you only want to print them out, then you probably don't need to keep the triples you've found so far around as I was assuming above. 更新:鉴于您刚刚添加的代码,如果您只想将它​​们打印出来,那么您可能不需要像我上面所假设的那样保留您到目前为止所发现的三元组。 The following modification to your code might work: 您的代码的以下修改可能有效:

for (k = 0; k < 50; k++)
{
    for ( i = k; i < 50; i++)
    {
        for ( j = i; j < 50; j++)
        {
            if ( (k+1)*(k+1) + (i+1)*(i+1) == (j+1)*(j+1) )
            {
                System.out.println( "\n\n\t\tThe numbers are : " + (k+1) + ", "
                                                                 + (i+1) + ", "
                                                                 + (j+1) );
            }
        }
    }
}

Note that I changed the starting point of the inner loops to ensure the following holds of all of the triples you will find: k <= i <= j . 请注意,我更改了内部循环的起点,以确保您将找到所有三元组的以下保持: k <= i <= j I believe that this constraint will also ensure uniqueness. 我相信这种约束也将确保唯一性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM