这里给出了几种形式来求。
勾股定理:
#include<iostream>
#include<cmath>//pow()和sqrt()函数需要
using namespace std;
int main(){
long long x,y,z;//z是要求的边
cin>>x>>y;//输入两条直角边
z=sqrt(pow(x,2)+pow(y,2));
/*
勾股定理:
a^2+b^2=c^2
在此题中
x^2+y^2=z^2
得出算法为sqrt(pow(x,2)+pow(y,2))
常规做法:
x*=x;
y*=y;
cout<<sqrt(x+y);
*/
cout<<z;
return 0;
}
STL函数做法:
#include<iostream>
#include<cmath>//hypot()函数需要
using namespace std;
int main(){
double x,y,z;//hypot()函数的参数必须是double类型
cin>>x>>y;//输入直角边长度
z=hypot(x,y);
/*
STL函数做法:
double hypot(double x,double y);
已知直角三角形两个直角边长度,求斜边长度
hypot()函数返回欧几里德范数sqrt(x*x + y*y);(勾股定理)
*/
cout<<z;
return 0;
}
希望大家看过这篇文章后会有更多的收获。