📜  打印总和等于 N 和 LCM 至多 N/2 的三元组

📅  最后修改于: 2021-10-25 06:21:49             🧑  作者: Mango

给定一个正整数N ,任务是找到一个三元组{X, Y, Z} ,使得XYZ 的最小公倍数小于或等于N/2并且XY 、并且Z等于N 。如果存在多个答案,则打印其中任何一个。

例子:

处理方法:可以根据以下事实解决问题:

请按照以下步骤解决问题:

  • 3 个变量x、yz初始化为0以存储三元组的值。
  • 如果N%2不等于0,N是奇数,则执行以下步骤:
    • 如果N是奇数,那么x、yz中至少有一个应该是奇数,并且在最坏的情况下, x、y、z的 LCM 是N/2
    • xy的值设置为N/2 ,将z的值设置为1。
  • 否则,如果N%4不等于0,z的值可以是2xy的值可以是N/2-1。否则, x的值可以是N/2yz的值可以是N/4。
  • 执行上述步骤后,打印x、yz 的值。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find a triplet  with the
// sum equal to N and LCM less than or
// equal to N
void validTriplet(int N)
{
    // Stores the triplets
    int x, y, z;
 
    // If N is odd
    if ((N % 2) != 0) {
        x = N / 2;
        y = N / 2;
        z = 1;
    }
    // If N is even
    else {
 
        // If N is not a multiple of 4
        if ((N % 4) != 0) {
            x = (N / 2) - 1;
            y = (N / 2) - 1;
            z = 2;
        }
 
        // If N is a multiple of 4
        else {
            x = N / 2;
            y = N / 4;
            z = N / 4;
        }
    }
 
    // Finally, print the answer
    cout << x << " " << y << " " << z << '\n';
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 5;
 
    // Function Call
    validTriplet(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find a triplet  with the
    // sum equal to N and LCM less than or
    // equal to N
    static void validTriplet(int N)
    {
       
        // Stores the triplets
        int x, y, z;
 
        // If N is odd
        if ((N % 2) != 0) {
            x = N / 2;
            y = N / 2;
            z = 1;
        }
       
        // If N is even
        else {
 
            // If N is not a multiple of 4
            if ((N % 4) != 0) {
                x = (N / 2) - 1;
                y = (N / 2) - 1;
                z = 2;
            }
 
            // If N is a multiple of 4
            else {
                x = N / 2;
                y = N / 4;
                z = N / 4;
            }
        }
 
        // Finally, print the answer
        System.out.print(x + " " + y + " " + z);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Given Input
        int N = 5;
 
        // Function Call
        validTriplet(N);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Function to find a triplet  with the
# sum equal to N and LCM less than or
# equal to N
def validTriplet(N):
   
     # If N is odd
    if (N % 2) != 0:
        x = N//2
        y = N//2
        z = 1
         
        # if N is even
    else:
       
         # If N is not a multiple of 4
        if (N % 4) != 0:
            x = N//2 - 1
            y = N//2 - 1
            z = 2
             
            # if N is multiple of 4
        else:
            x = N//2
            y = N//4
            z = N//4
             
     # Print the result
    print(str(x) + " " + str(y) + " " + str(z))
 
# Driver code
N = 5
validTriplet(N)
 
# This code is contributed by Parth Manchanda


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find a triplet  with the
// sum equal to N and LCM less than or
// equal to N
static void validTriplet(int N)
{
   
    // Stores the triplets
    int x, y, z;
 
    // If N is odd
    if ((N % 2) != 0) {
        x = N / 2;
        y = N / 2;
        z = 1;
    }
    // If N is even
    else {
 
        // If N is not a multiple of 4
        if ((N % 4) != 0) {
            x = (N / 2) - 1;
            y = (N / 2) - 1;
            z = 2;
        }
 
        // If N is a multiple of 4
        else {
            x = N / 2;
            y = N / 4;
            z = N / 4;
        }
    }
 
    // Finally, print the answer
    Console.Write(x + " " + y + " " + z );
}
 
// Driver Code
public static void Main()
{
    // Given Input
    int N = 5;
 
    // Function Call
    validTriplet(N);
     
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出
2 2 1

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