您的位置:首页 > 其它

输出三边在500以内的能构成直角三角形的数列

2017-05-24 19:13 337 查看
#include<iostream>
#include<cstring>
using namespace std;

//判断是否是直角三角形
bool isHardTriangle(int side1,int side2,int hypotenuse);

int main()
{
struct Triangle
{
int side1;
int side2;
int hypotenuse;
int flag;//决定该条数据是否显示 1为显示 0不显示
};

Triangle arr[500];

int counter=0;
for(int side1=3;side1<=500;side1++)
{
for(int side2=4;side2<=500;side2++)
{
for(int hypotenuse=5;hypotenuse<=500;hypotenuse++)
{
if(isHardTriangle(side1,side2,hypotenuse))
{
cout<<side1<<"\t"<<side2<<"\t"<<hypotenuse<<endl;
arr[counter].side1=side1;
arr[counter].side2=side2;
arr[counter].hypotenuse=hypotenuse;
arr[counter].flag=1;
counter++;
}
}
}
}

cout<<endl<<endl;

return 0;
}

bool isHardTriangle(int side1,int side2,int hypotenuse)
{
bool isHardTriangle=false;

if(side1*side1+side2*side2==hypotenuse*hypotenuse)
{
isHardTriangle=true;
}

return isHardTriangle;
}


运行效果如下



显然,这是有不足之处的。

如,{5,12,13},{12,5,13}表示的是同一个三角形。最佳的输出效果是不输出这些相同的序列。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐