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

📅  最后修改于: 2021-06-25 10:07:11             🧑  作者: 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的值至少为0并且A可被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)