给定一个整数N,我们需要找到三个整数(X,Y,Z),它们可以形成一个三角形,条件如下:
null
- 边的长度是不超过N的整数。
- 三边的异或为0,即X^Y^Z=0
- 三角形的面积大于0。
找出满足上述条件的所有可能的三元组。 例如:
Input: 6Output: 6 5 3 Input: 10Output: 10 9 3 6 5 3
天真的方法: 通过从N迭代到1来选择第一面,然后通过从第一面迭代到1来选择第二面,然后通过从第二面迭代到1来选择第三面。现在检查三条边是否可以构成三角形(两条较小边的和必须大于最长边),并且长度的xor和等于0。 时间完整性=O(n^3)。 有效方法: 在这种方法中,我们像在第一种方法中一样选择前两条边,第三条边将等于前两条边的xor(这将使长度的xor和等于0),这条边必须小于前两条边。现在检查这些边是否可以形成三角形。 时间复杂度=O(n^2)
C++
// C++ implementation to find all possible // triangles with XOR of sides zero #include <bits/stdc++.h> 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
<?php // PHP implementation to find all possible // triangles with XOR of sides zero // function to find all triples which // satisfy the necessary condition function find_all_possible_sides( $n ) { // selects first side for ( $x = $n ; $x > 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 , "" ; } } } } } // Driver Code $n = 10; find_all_possible_sides( $n ); // This code is contributed by anuj_67 ?> |
Javascript
<script> // Javascript implementation to find all possible // triangles with XOR of sides zero // function to find all triples which // satisfy the necessary condition function find_all_possible_sides(n) { // selects first side for (let x = n; x > 0; x--) { // select second side for (let y = x - 1; y >= 0; y--) { // third side is equal to xor of // first and second side let z = x ^ y; if (z < x && z < y) { // find longest side let max_side = Math.max(x, Math.max(y, z)); // check if it can make a triangle if (x + y + z - max_side > max_side) { document.write(x + " " + y + " " + z + "<br>" ); } } } } } // Driver Program let n = 10; find_all_possible_sides(n); </script> |
输出:
10 9 36 5 3
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END