📌  相关文章
📜  找到一个三元组 (A, B, C) 使得 3*A + 5*B + 7*C 等于 N

📅  最后修改于: 2021-10-26 06:35:04             🧑  作者: Mango

给定一个整数N ,任务是找到三个正整数ABC ,使得表达式(3*A + 5*B + 7*C) 的值等于N 。如果不存在这样的三元组,则打印“-1”

例子:

朴素的方法:解决问题的最简单方法是生成所有可能的整数为N 的三元组,并检查是否存在任何三元组 (A, B, C),使得(3*A + 5*B + 7*C)等于N 。如果发现是真的,则打印该三元组。否则,打印“-1”
时间复杂度: O(N 3 )
辅助空间: O(1)

高效的方法:可以根据以下观察优化上述方法,即A的值位于[0, N / 3]范围内, B的值位于[0, N / 5]范围内,并且该值的C位于[0, N / 7]范围内。请按照以下步骤解决问题:

  • 迭代范围[0, N/7]并执行以下操作:
    • 迭代范围[0, N/5]并找到A的值(N – 5*j – 7*i)
    • 在上述步骤中,如果A的值至少为0A可被3整除,则存在(A/3, i, j)这样的三元组。打印这个三元组并跳出循环。
  • 完成上述步骤后,如果不存在这样的三元组,则打印“-1”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find a triplet (A, B, C)
// such that 3 * A + 5 * B + 7 * C is N
void CalculateValues(int N){
 
  int A = 0, B = 0, C = 0;
 
  // Iterate over the range [0, N//7]
  for (C = 0; C < N/7; C++)
  {
 
    // Iterate over the range [0, N//5]
    for ( B = 0; B < N/5; B++)
    {
 
      // Find the value of A
      int A = N - 7 * C - 5 * B;
 
      // If A is greater than or equal
      // to 0 and divisible by 3
      if (A >= 0 && A % 3 == 0)
      {
        cout << "A = " << A / 3 << ", B = " << B << ", C = "<< C << endl;
 
        return;
      }
    }
  }
 
  // Otherwise, print -1
  cout << -1 << endl;
 
}
 
// Driver Code
int main()
{
  int N = 19;
  CalculateValues(19);
 
  return 0;
}
 
// This code is contributed by susmitakundugoaldanga.


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
  // Function to find a triplet (A, B, C)
  // such that 3 * A + 5 * B + 7 * C is N
  static void CalculateValues(int N)
  {
    int A = 0, B = 0, C = 0;
 
    // Iterate over the range [0, N//7]
    for (C = 0; C < N/7; C++)
    {
 
      // Iterate over the range [0, N//5]
      for ( B = 0; B < N/5; B++)
      {
 
        // Find the value of A
        A = N - 7 * C - 5 * B;
 
        // If A is greater than or equal
        // to 0 and divisible by 3
        if (A >= 0 && A % 3 == 0)
        {
          System.out.print("A = " + A / 3 + ", B = " + B + ", C = "+ C);
 
          return;
        }
      }
    }
 
    // Otherwise, print -1
    System.out.println(-1);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 19;
    CalculateValues(19);
  }
}
 
// This code is contributed by souravghosh0416.


Python3
# Python program for the above approach
 
# Function to find a triplet (A, B, C)
# such that 3 * A + 5 * B + 7 * C is N
def CalculateValues(N):
   
    # Iterate over the range [0, N//7]
    for C in range(0, N//7 + 1):
         
        # Iterate over the range [0, N//5]
        for B in range(0, N//5 + 1):
 
            # Find the value of A
            A = N - 7 * C - 5 * B
 
            # If A is greater than or equal
            # to 0 and divisible by 3
            if (A >= 0 and A % 3 == 0):
                print("A =", A / 3, ", B =", B, ", \
                       C =", C, sep =" ")
                return
     
    # Otherwise, print -1
    print(-1)
    return
 
 
# Driver Code
if __name__ == '__main__':
   
    N = 19
    CalculateValues(19)


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to find a triplet (A, B, C)
  // such that 3 * A + 5 * B + 7 * C is N
  static void CalculateValues(int N)
  {
    int A = 0, B = 0, C = 0;
 
    // Iterate over the range [0, N//7]
    for (C = 0; C < N/7; C++)
    {
 
      // Iterate over the range [0, N//5]
      for ( B = 0; B < N/5; B++)
      {
 
        // Find the value of A
        A = N - 7 * C - 5 * B;
 
        // If A is greater than or equal
        // to 0 and divisible by 3
        if (A >= 0 && A % 3 == 0)
        {
          Console.Write("A = " + A / 3 + ", B = " + B + ", C = "+ C);
 
          return;
        }
      }
    }
 
    // Otherwise, print -1
    Console.WriteLine(-1);
  }
 
  // Driver Code
  static public void Main()
  {
    int N = 19;
    CalculateValues(19);
  }
}
 
// This code is contributed by splevel62.


Javascript


输出
A = 3, B = 2, C = 0

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