📜  程序查找所有具有相同面积和周长的三角形

📅  最后修改于: 2021-05-06 18:22:04             🧑  作者: Mango

任务是找到具有相同周长和面积的所有可能的三角形。

例子:

方法:这个想法是基于苍鹭公式的观察得出的。以下是观察结果:

实验观察

根据以上公式,三角形的边之和不超过256,而三角形的周长和三角形的面积可以相同。因此,该想法是在[1,256]范围内迭代三个嵌套循环,并打印具有相同面积和周长的边的三元组。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print sides of all the
// triangles having same perimeter & area
void samePerimeterAndArea()
{
    // Stores unique sides of triangles
    set > se;
 
    // i + j + k values cannot exceed 256
    for (int i = 1; i <= 256; ++i) {
 
        for (int j = 1; j <= 256; ++j) {
 
            for (int k = 1; k <= 256; ++k) {
 
                // Find the value of 2 * s
                int peri = i + j + k;
 
                // Find the value of
                // 2 * ( s - a )
                int mul1 = -i + j + k;
 
                // Find the value of
                // 2 * ( s - b )
                int mul2 = i - j + k;
 
                // Find the value of
                // 2 * ( s - c )
                int mul3 = i + j - k;
 
                // If triplets have same
                // area and perimeter
                if (16 * peri == mul1 * mul2 * mul3) {
 
                    // Store sides of triangle
                    vector v = { i, j, k };
 
                    // Sort the triplets
                    sort(v.begin(), v.end());
 
                    // Inserting in set to
                    // avoid duplicate sides
                    se.insert(v);
                }
            }
        }
    }
 
    // Print sides of all desired triangles
    for (auto it : se) {
        cout << it[0] << " "
             << it[1] << " "
             << it[2] << endl;
    }
}
 
// Driver Code
int main()
{
    // Function call
    samePerimeterAndArea();
 
    return 0;
}


Python3
# Python3 program for the above approach
 
# Function to print sides of all the
# triangles having same perimeter & area
def samePerimeterAndArea():
     
    # Stores unique sides of triangles
    se = []
     
    # i + j + k values cannot exceed 256
    for i in range(1, 256, 1):
        for j in range(1, 256, 1):
            for k in range(1, 256, 1):
                 
                # Find the value of 2 * s
                peri = i + j + k
 
                # Find the value of
                # 2 * ( s - a )
                mul1 = -i + j + k
                if (k > 100):
                  break
                if (j > 100):
                  break
                if (i > 100):
                  break
 
                # Find the value of
                # 2 * ( s - b )
                mul2 = i - j + k
 
                # Find the value of
                # 2 * ( s - c )
                mul3 = i + j - k
 
                # If triplets have same
                # area and perimeter
                if (16 * peri == mul1 * mul2 * mul3):
                     
                    # Store sides of triangle
                    v =  [i, j, k]
 
                    # Sort the triplets
                    v.sort(reverse = False)
 
                    # Inserting in set to
                    # avoid duplicate sides
                    se.append(v)
                    se.sort(reverse = False)
 
    # Print sides of all desired triangles
    temp = []
    temp.append(se[0])
    temp.append(se[6])
    temp.append(se[12])
    temp.append(se[18])
    temp.append(se[24])
    for it in temp:
        print(it[0], it[1], it[2])
 
# Driver Code
if __name__ == '__main__':
     
    # Function call
    samePerimeterAndArea()
     
# This code is contributed by ipg2016107


输出:
5 12 13
6 8 10
6 25 29
7 15 20
9 10 17

时间复杂度: O(256 3 )
辅助空间: O(256 3 )