找到独特的勾股数 [英] find unique pythagorean triples

查看:177
本文介绍了找到独特的勾股数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从序列排除重复三角形的勾股数刚刚打印的三角形为100斜边,但我在排除部分未能做到......我有什么如下:

I am trying to exclude the repeat triangles from a sequence of Pythagorean triples which just prints triangles with hypotenuses of 100, but what I do in the exclusion part fails... I have what follows:

....
    int one_hundred = 0,
     if( (a*a) + (b*b) == (h*h) ){

        //exclusion
        if((a == b)){

           continue;

        }else {

       //Just prints the the triangles that have hypotenuses of 100
      if(h == 100){
            cout <<  a << "     " << b << "     " << h << endl;

           }

      .....

     }

现在的输出,这应该是

我的意思是重复的第一个和最后行具有相同的一组对边的

What I mean by repeat is that the first and the last rows have the same set of pairs of sides

我想有什么像这样的输出:

What I would like to is an output like this:

但这个我做如下:

//看它们是否重复

if((a == 96)){

    continue;

    }

和我认为,通过比较== b口,也可以达到相同的,但我没有:

And I thought that by comparing a == b I would have achieved the same but I did not:

if((a == b)){

    continue;

    }

希望这阐明什么,我试图做...

Hopefully this clarifies what I am trying to do...

再次感谢您!

推荐答案

我想一个简单的方法是创建一个的std ::矢量&lt;&布尔GT;状态(H); 因为 ^ h 是最大四面八方的人绝不可能较大;对?我们知道乘以本身可能等于另一个乘以本身没有其他的数字,否则你可能会说类似的 5 * 5 = 4 * 4 !所以,你可以使用向量下标为数字再presentatives。当你拉一数,发现这是你的问题的解决方案简单地切换自己的状态。整个事情会是这样的 -

I think a simple approach would be to create a std::vector<bool> state(h); Because h is the largest of all sides the others could never be larger; right? And we know no other number multiplied by itself could equal another multiplied by itself, otherwise you could say something like 5*5 = 4*4! So, you can use the vector subscripts as representatives of the numbers. As you pull one number and find it is a solution to your problem simply switch its state to false. The whole thing would go something like this -

bool loop;
std::vector<bool> *state = new std::vector<bool>(h, true);
for(int i = 2; i != sizeof(bool) * h; ++i)
{
    if((state + i))
    {
        a = i;
        loop = true;

        for(int j = i + 1; loop && j != sizeof(bool) * h; ++j)
        {
            if((state + j))
            {
                b = j;          

                if((a*a) + (b*b) == (h*h))
                {
                    loop = false;
                    (*state)[i] = false;                                                      
                    (*state)[j] = false;
                    std::cout << a << " " << b << " " << h << std::endl;
                }
            }
        }
    }
}

我的输出是:

28 96 100
60 80 100

这是我认为你在你想要的结果的例子的意思。否则,你需要创建一个有双重打印特定的一些规则。在这种情况下,你可以添加一个开关(){} 。不用这么辛苦,授予它不是所有的优雅,imoo。

Which is what I think you meant in your desired results example. Otherwise you need to create a rule that does double print certain ones. In that case you could add a switch(){}. Not so hard, granted it's not all that elegant, imoo.

注意:
这真的是没有必要改变语句&gt;对于(I)是递增的,将永远不会再感动。但它的存在在你需要以后使用的潜艇情况。指数在0和1已经被忽略不三角形的侧面可以是0,如果一个是可行的对方必须等于 ^ h 所以不要浪费precious时间。如果你确实持有它们记住0和1 真正来开始,并应设置为false。

Note: It really isn't necessary to change state->at(i) to false for i is incremented and will never be touched again. But it's there in case you'd need to use the subs later on. Indexes 0 and 1 have been ignored for no side of a triangle can be 0 and if one was viable the other side would have to equal h so don't waste precious time. If you do indeed hold on to them remember that 0 and 1 are true to begin with and should be set to false.

这篇关于找到独特的勾股数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆