📌  相关文章
📜  没有元素可被3和N整除的三元组

📅  最后修改于: 2021-04-26 08:28:49             🧑  作者: Mango

给定大于2的整数N,任务是打印a,b和c的任意组合,以便:

例子:

Input: N = 233
Output: 77 77 79

Input: N = 3
Output: 1 1 1

天真的方法是使用三个循环并检查给定条件。

下面是上述方法的实现:

C++
// C++ program to print a, b and c
// such that a+b+c=N
#include 
using namespace std;
 
// Function to print a, b and c
void printCombination(int n)
{
 
    // first loop
    for (int i = 1; i < n; i++) {
 
        // check for 1st number
        if (i % 3 != 0) {
 
            // second loop
            for (int j = 1; j < n; j++) {
 
                // check for 2nd number
                if (j % 3 != 0) {
 
                    // third loop
                    for (int k = 1; k < n; k++) {
 
                        // Check for 3rd number
                        if (k % 3 != 0 && (i + j + k) == n) {
                            cout << i << " " << j << " " << k;
                            return;
                        }
                    }
                }
            }
        }
    }
}
 
// Driver Code
int main()
{
    int n = 233;
 
    printCombination(n);
    return 0;
}


Java
// Java program to print a,
// b and c such that a+b+c=N
import java.io.*;
 
class GFG
{
 
// Function to print a, b and c
static void printCombination(int n)
{
    // first loop
    for (int i = 1; i < n; i++)
    {
 
        // check for 1st number
        if (i % 3 != 0)
        {
 
            // second loop
            for (int j = 1; j < n; j++)
            {
 
                // check for 2nd number
                if (j % 3 != 0)
                {
 
                    // third loop
                    for (int k = 1; k < n; k++)
                    {
 
                        // Check for 3rd number
                        if (k % 3 != 0 && (i + j + k) == n)
                        {
                            System.out.println( i + " " +
                                                j + " " + k);
                            return;
                        }
                    }
                }
            }
        }
    }
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 233;
     
    printCombination(n);
}
}
 
// This code is contributed
// by anuj_67.


Python3
# Python3 program to print a, b
# and c such that a+b+c=N
 
# Function to print a, b and c
def printCombination(n):
 
    # first loop
    for i in range(1, n):
     
        # check for 1st number
        if (i % 3 != 0):
             
            # second loop
            for j in range(1, n):
 
                # check for 2nd number
                if (j % 3 != 0):
 
                    # third loop
                    for k in range(1, n):
 
                        # Check for 3rd number
                        if (k % 3 != 0 and
                           (i + j + k) == n):
                            print(i, j, k);
                            return;
 
# Driver Code
n = 233;
 
printCombination(n);
 
# This code is contributed
# by mits


C#
// C# program to print a,
// b and c such that a+b+c=N
class GFG
{
 
// Function to print a, b and c
static void printCombination(int n)
{
    // first loop
    for (int i = 1; i < n; i++)
    {
 
        // check for 1st number
        if (i % 3 != 0)
        {
 
            // second loop
            for (int j = 1; j < n; j++)
            {
 
                // check for 2nd number
                if (j % 3 != 0)
                {
 
                    // third loop
                    for (int k = 1; k < n; k++)
                    {
 
                        // Check for 3rd number
                        if (k % 3 != 0 && (i + j + k) == n)
                        {
                            System.Console.WriteLine(i + " " +
                                                     j + " " + k);
                            return;
                        }
                    }
                }
            }
        }
    }
}
 
// Driver Code
static void Main ()
{
    int n = 233;
     
    printCombination(n);
}
}
 
// This code is contributed
// by mits


PHP


Javascript


C++
// C++ program to print a, b and c
// such that a+b+c=N
#include 
using namespace std;
 
// Function to print a, b and c
void printCombination(int n)
{
    cout << 1 << " ";
 
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        cout << 2 << " " << n - 3;
    else
        cout << 1 << " " << n - 2;
}
 
// Driver Code
int main()
{
    int n = 233;
 
    printCombination(n);
    return 0;
}


Java
// Java program to print a, b
// and c such that a+b+c=N
class GFG
{
// Function to print a, b and c
static void printCombination(int n)
{
    System.out.print(1 + " ");
 
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        System.out.print(2 + " " + (n - 3));
    else
        System.out.print(1 + " " + (n - 2));
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 233;
 
    printCombination(n);
}
}
 
// This code is contributed by mits


Python3
# Python3 program to print a, b and c
# such that a+b+c=N
 
# Function to print a, b and c
def printCombination(n):
    print("1 ",end="");
 
    # check if n-2 is divisible
    # by 3 or not
    if ((n - 2) % 3 == 0):
        print("2",n - 3,end="");
    else:
        print("1",(n - 2),end="");
 
# Driver code
if __name__=='__main__':
    n = 233;
 
    printCombination(n);
 
# This code is contributed by mits


C#
// C# program to print a, b
// and c such that a+b+c=N
 
class GFG
{
// Function to print a, b and c
static void printCombination(int n)
{
    System.Console.Write(1 + " ");
 
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        System.Console.Write(2 + " " + (n - 3));
    else
        System.Console.Write(1 + " " + (n - 2));
}
 
// Driver Code
static void Main()
{
    int n = 233;
 
    printCombination(n);
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
1 2 230

时间复杂度: O(n 3 )

高效的方法:

  1. 将1视为三个数字之一。
  2. 其他两个数字将是:
    • (2,n-3)如果n-2可被3整除。
    • 否则(1,n-2)如果n-2不能被3整除。

下面是上述方法的实现:

C++

// C++ program to print a, b and c
// such that a+b+c=N
#include 
using namespace std;
 
// Function to print a, b and c
void printCombination(int n)
{
    cout << 1 << " ";
 
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        cout << 2 << " " << n - 3;
    else
        cout << 1 << " " << n - 2;
}
 
// Driver Code
int main()
{
    int n = 233;
 
    printCombination(n);
    return 0;
}

Java

// Java program to print a, b
// and c such that a+b+c=N
class GFG
{
// Function to print a, b and c
static void printCombination(int n)
{
    System.out.print(1 + " ");
 
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        System.out.print(2 + " " + (n - 3));
    else
        System.out.print(1 + " " + (n - 2));
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 233;
 
    printCombination(n);
}
}
 
// This code is contributed by mits

Python3

# Python3 program to print a, b and c
# such that a+b+c=N
 
# Function to print a, b and c
def printCombination(n):
    print("1 ",end="");
 
    # check if n-2 is divisible
    # by 3 or not
    if ((n - 2) % 3 == 0):
        print("2",n - 3,end="");
    else:
        print("1",(n - 2),end="");
 
# Driver code
if __name__=='__main__':
    n = 233;
 
    printCombination(n);
 
# This code is contributed by mits

C#

// C# program to print a, b
// and c such that a+b+c=N
 
class GFG
{
// Function to print a, b and c
static void printCombination(int n)
{
    System.Console.Write(1 + " ");
 
    // check if n-2 is divisible
    // by 3 or not
    if ((n - 2) % 3 == 0)
        System.Console.Write(2 + " " + (n - 3));
    else
        System.Console.Write(1 + " " + (n - 2));
}
 
// Driver Code
static void Main()
{
    int n = 233;
 
    printCombination(n);
}
}
 
// This code is contributed by mits

的PHP


Java脚本


输出:
1 2 230

时间复杂度: O(1)