C++ 如何在C++中检查3条边是否形成三角形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19835174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if 3 sides form a triangle in C++
提问by user2943407
I am trying to check if 3 sides form a triangle in C++, but the answer for all possible numbers I tried it says wrong...
我试图检查 3 条边是否在 C++ 中形成一个三角形,但是我尝试过的所有可能数字的答案都是错误的......
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
if (pow(a,2) == pow(b,2) * pow(c,2) || pow(b,2) == pow(a,2) * pow(c,2) || pow(c,2) == pow(a,2) * pow(b,2))
cout << "The sides form a triangle" << endl;
else
cout << "The sides do not form a triangle." << endl;
return 0;
}
回答by david
Let's say that a, b, c is the sides of the triangle. Therefore, it must be satisfy this criteria :
假设 a, b, c 是三角形的边。因此,它必须满足以下条件:
- a + b > c
- a + c > b
- b + c > a
- a + b > c
- a + c > b
- b + c > a
All the criteria must be true. If one of them are false, then a, b, c will not create the triangle.
所有的标准都必须是真实的。如果其中之一为假,则 a、b、c 将不会创建三角形。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
// check whether a, b, c can form a triangle
if (a+b > c && a+c > b && b+c > a)
cout << "The sides form a triangle" << endl;
else
cout << "The sides do not form a triangle." << endl;
return 0;
}
回答by HVar
Triangle conditions to check for,
要检查的三角形条件,
(a + b > c),
(b + c > a),
(c + a > b)
回答by niko
For a normal triangle
对于普通三角形
1. sum of any two sides is greater than third side (or)
2. difference of any two sides is less than third side
hint : a+b > c || ...
For a right angled triangle
对于直角三角形
1) sum of the squares of two sides equals the square of the longest side
Hint:
暗示:
Find the longest side of three sides, that is find longest number in the three..
square the remaining two nums, add them and equate it to square of longest number
回答by shuttle87
Assuming you are only testing for right angled triangles then the logic to use is z^2 = x^2 + y+2 So there's a mistake in the logic:
假设你只测试直角三角形,那么使用的逻辑是 z^2 = x^2 + y+2 所以逻辑中有一个错误:
if (pow(a,2) == pow(b,2) * pow(c,2) || pow(b,2) == pow(a,2) * pow(c,2) || pow(c,2) == pow(a,2) * pow(b,2))
This should be:
这应该是:
if (pow(a,2) == pow(b,2) + pow(c,2) || pow(b,2) == pow(a,2) + pow(c,2) || pow(c,2) == pow(a,2) + pow(b,2))
But even with this change the result might be might wrong due to testing equality on floating point numbers. Make a specific function to test 2 floating point numbers are close enough given some tolerance you decide on then use that for comparisons.
但即使进行了这种更改,由于在浮点数上测试相等性,结果也可能是错误的。制作一个特定的函数来测试 2 个浮点数是否足够接近,给定一些您决定的容差,然后将其用于比较。
If you do not want to limit your approach to only right angled triangles then you might wish to read up on the triangle inequality. In summary the triangle inequality just states that the length of any edge in a triangle must be smaller than the sum of the other 2 edges.
如果您不想将您的方法仅限于直角三角形,那么您可能希望阅读三角形不等式。总之,三角形不等式只是说明三角形中任何边的长度必须小于其他 2 条边的总和。
回答by Divyanshi
An efficient approach will be to sort the given sides. This will be efficient if you are given an entire array and you are asked whether the given array elements form a triangle or not. This can be applied for n number of given sides. However, this can also be applied for 3 sides. Suppose the given array is b. In your case array b is of length h=3.
一种有效的方法是对给定的边进行排序。如果给你一个完整的数组并且你被问到给定的数组元素是否形成一个三角形,这将是有效的。这可以应用于 n 个给定的边。但是,这也可以应用于 3 个面。假设给定的数组是 b。在您的情况下,数组 b 的长度为 h=3。
sort(b,b+h);
for (int j=0;j<(h-2);j++){
if (b[j]+b[j+1]>b[j+2])
{
return true;
}
}
else {
return false;
}
回答by Al Xorizmi
Actually, given any three sides, you only need to check one condition: that the longest side, say c, is less than the sum of the two shorter sides, say a and b. That is,
实际上,给定任意三个边,您只需要检查一个条件:最长的边(例如 c)小于两条较短边(例如 a 和 b)的总和。那是,
if c < a+b {
return true;
} else return false;
This is the essence of the triangle inequality theorem. The other conditions will be trivially true when it is a triangle and irrelevant if this one condition is not. The key, of course, will be to sort the three sides to find the longest side by using a simple sorting algorithm. The assumption in the code is that the sides have already been sorted so that c is the longest.
这就是三角不等式定理的本质。当它是三角形时,其他条件将是微不足道的,如果这一个条件不是,则无关紧要。当然,关键是使用简单的排序算法对三个边进行排序以找到最长的边。代码中的假设是边已经排序,所以 c 是最长的。