📜  找出所有边为零的异或三角形

📅  最后修改于: 2022-05-13 01:57:58.654000             🧑  作者: Mango

找出所有边为零的异或三角形

给定一个整数 N,我们需要找到三个整数 (X, Y, Z),它们可以组成一个三角形,满足以下条件:

  • 边长是不超过 N 的整数。
  • 三边异或为0,即X ^ Y ^ Z = 0
  • 三角形的面积大于 0。

找出满足上述条件的所有可能的三元组。
例子:

Input:  6
Output: 6 5 3 

Input:  10
Output: 10 9 3
        6 5 3

朴素方法:通过从 N 迭代到 1 来选择第一条边,然后通过从第一条边迭代到 1 来选择第二条边,然后通过从第二条边迭代到 1 来选择第三条边。现在检查三个边是否可以组成一个三角形(两个较小边的总和必须大于最长边)并且长度的异或和等于 0。
时间复杂度 = O(n^3)。
有效方法:在这种方法中,我们像第一种方法一样选择前两条边,第三条边将等于前两条边的异或(这将使长度的异或和等于 0)和这条边必须小于前两条边。现在检查这些边是否可以组成一个三角形。
时间复杂度 = O(n^2)

C++
// C++ implementation to find all possible
// triangles with XOR of sides zero
#include 
using namespace std;
 
// function to find all triples which
// satisfy the necessary condition
void find_all_possible_sides(int n) {
 
  // selects first side
  for (int x = n; x > 0; x--) {
 
    // select second side
    for (int y = x - 1; y >= 0; y--) {
 
      // third side is equal to xor of
      // first and second side
      int z = x ^ y;
      if (z < x && z < y) {
 
        // find longest side
        int max_side = max(x, max(y, z));
 
        // check if it can make a triangle
        if (x + y + z - max_side > max_side) {
           cout << x << " " << y << " "
                << z << endl;
        }
      }
    }
}
 
}
 
// Driver Program
int main() {
  int n = 10;
  find_all_possible_sides(n);
  return 0;
}


Java
// Java implementation to find all possible
// triangles with XOR of sides zero
import java.lang.*;
 
class GFG {
     
// function to find all triples which
// satisfy the necessary condition
static void find_all_possible_sides(int n) {
     
    // selects first side
    for (int x = n; x > 0; x--) {
 
    // select second side
    for (int y = x - 1; y >= 0; y--) {
 
        // third side is equal to xor of
        // first and second side
        int z = x ^ y;
        if (z < x && z < y) {
 
        // find longest side
        int max_side = Math.max(x, Math.max(y, z));
 
        // check if it can make a triangle
        if (x + y + z - max_side > max_side) {
            System.out.println(x + " " + y + " " + z);
        }
        }
    }
    }
}
 
// Driver code
public static void main(String[] args) {
     
    int n = 10;
    find_all_possible_sides(n);
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# function to find
# all triples which
# satisfy the necessary condition
def find_all_possible_sides(n):
     
    # selects first side
    for x in range(n,0,-1):
 
        # select second side
        for y in range(x - 1,-1,-1):
     
            # third side is equal to xor of
            # first and second side
            z = x ^ y
            if (z < x and z < y):
            
                # find longest side
                max_side =max(x,max(y, z))
 
                # check if it can make a triangle
                if (x + y + z - max_side > max_side):
         
                    print(x , " " , y , " ",
                                z)
                                 
# driver code                               
 
n = 10
find_all_possible_sides(n)
 
# This code is contributed
# by Anant Agarwal.


C#
// C# implementation to find all possible
// triangles with XOR of sides zero
using System;
 
class GFG {
     
    // function to find all triples which
    // satisfy the necessary condition
    static void find_all_possible_sides(int n) {
         
        // selects first side
        for (int x = n; x > 0; x--) {
     
            // select second side
            for (int y = x - 1; y >= 0; y--) {
         
                // third side is equal to xor of
                // first and second side
                int z = x ^ y;
                if (z < x && z < y) {
         
                    // find longest side
                    int max_side = Math.Max(x,
                                 Math.Max(y, z));
             
                    // check if it can make a
                    // triangle
                    if (x + y + z - max_side >
                                     max_side) {
                                          
                        Console.WriteLine(x + " "
                                  + y + " " + z);
                    }
                }
            }
        }
    }
     
    // Driver code
    public static void Main() {
         
        int n = 10;
         
        find_all_possible_sides(n);
    }
}
 
// This code is contributed by vt_m.


PHP
 0; $x--) {
 
    // select second side
    for ($y = $x - 1; $y >= 0; $y--) {
 
    // third side is equal to xor of
    // first and second side
    $z = $x ^ $y;
    if ($z < $x && $z < $y) {
 
        // find longest side
        $max_side = max($x, max($y, $z));
 
        // check if it can make a triangle
        if ($x + $y + $z - $max_side > $max_side)
        {
              echo $x , " " ,$y , " ",
                   $z ,"\n" ;
        }
    }
    }
}
 
}
 
// Driver Code
$n = 10;
find_all_possible_sides($n);
 
// This code is contributed by anuj_67
?>


Javascript


输出:

10 9 3
6 5 3