📜  如果任意一个乘以 M,则找到 M,其中 A、B、C 按给定顺序构成 AP

📅  最后修改于: 2022-05-13 01:56:04.833000             🧑  作者: Mango

如果任意一个乘以 M,则找到 M,其中 A、B、C 按给定顺序构成 AP

给定 3 个正整数A、B 和 C。选择一个正整数M ,并将 A、B 或 C 中的任何一个与 M 相乘。任务是决定 A、B 和 C 是否符合算术级数平均 (AP)表示执行上述操作一次后。 A、B、C 的顺序不能更改。

例子:

方法:

  • 如果三个整数在 AP 中

以下是使用这种关系来确定给定的三个数字是否可以形成 AP 的步骤-

  • 声明一个变量new_b并将其初始化为2 * B
  • 如果A、B、C已经在 AP 中,则返回true 。它们中的任何一个都可以乘以 M=1,答案总是“”。
  • 如果 B 可以乘以任何数字以使其等于 A+C 即(A + C) % new_b = 0 ,则返回true 。无论M的值如何,余数始终为 0。
  • 现在剩下的唯一选择是将AC相乘以使它们的总和等于new_b
    • 如果(A + C) > new_b,则返回false 。在这种情况下,将ACM相乘只会增加其值。答案永远是“”。
    • 否则,如果(new_b – C) % A = 0 ,则返回true 。在这种情况下, A可以乘以某个整数M 。在这种情况下, (new_b – C) 可以看作是A的倍数。因此,在(new_b – C)除以A时,无论 M 的值如何,余数始终为0。答案将是“”(根据等式 (2) 的 a)。
    • 否则,如果(new_b – A) % B = 0 ,则返回true 。在这种情况下, C可以乘以某个整数M 。在这种情况下, (new_b – A) 可以看作是C的倍数。因此,在(new_b – A)除以C时,余数将始终为0 ,而与M的值无关。答案将是“”(根据等式(3))。
    • 否则返回false

插图:

以下是上述方法的实现 -

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if A, B, C
// can be in A.P. after multiplying
// by any integer M
bool isAP(int A, int B, int C)
{
    int new_b = B * 2;
 
    // Check if A, B, C are already
    // in A.P.
    if (new_b == (A + C))
        return true;
 
    // Check if multiplying B will
    // give A.P.
    if ((A + C) % new_b == 0)
        return true;
 
    if ((A + C) > new_b)
        return false;
 
    // Check if multiplying A will
    // give A.P.
    if ((new_b - C) % A == 0)
        return true;
 
    // Check if multiplying C will
    // give A.P.
    if ((new_b - A) % C == 0)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int A, B, C;
    A = 30;
    B = 5;
    C = 10;
 
    // Function call
    bool ans = isAP(A, B, C);
 
    // Displaying the answer
    if (ans)
        cout << "YES";
    else
        cout << "NO";
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to check if A, B, C
  // can be in A.P. after multiplying
  // by any integer M
  static Boolean isAP(int A, int B, int C)
  {
    int new_b = B * 2;
 
    // Check if A, B, C are already
    // in A.P.
    if (new_b == (A + C))
      return true;
 
    // Check if multiplying B will
    // give A.P.
    if ((A + C) % new_b == 0)
      return true;
 
    if ((A + C) > new_b)
      return false;
 
    // Check if multiplying A will
    // give A.P.
    if ((new_b - C) % A == 0)
      return true;
 
    // Check if multiplying C will
    // give A.P.
    if ((new_b - A) % C == 0)
      return true;
 
    return false;
  }
 
  // Driver code
  public static void main (String[] args) {
    int A, B, C;
    A = 30;
    B = 5;
    C = 10;
 
    // Function call
    Boolean ans = isAP(A, B, C);
 
    // Displaying the answer
    if (ans)
      System.out.print("YES");
    else
      System.out.print("NO");
  }
}
 
// This code is contributed by hrithikgarg03188


Python3
# Python code for the above approach
 
# Function to check if A, B, C
# can be in A.P. after multiplying
# by any integer M
def isAP(A, B, C):
    new_b = B * 2
 
    # Check if A, B, C are already
    # in A.P.
    if (new_b == (A + C)):
        return True
 
    # Check if multiplying B will
    # give A.P.
    if ((A + C) % new_b == 0):
        return True
 
    if ((A + C) > new_b):
        return False
 
    # Check if multiplying A will
    # give A.P.
    if ((new_b - C) % A == 0):
        return True
 
    # Check if multiplying C will
    # give A.P.
    if ((new_b - A) % C == 0):
        return True
 
    return False
 
# Driver code
A = 30
B = 5
C = 10
 
# Function call
ans = isAP(A, B, C)
 
# Displaying the answer
if (ans):
    print("YES")
else:
    print("NO")
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to check if A, B, C
  // can be in A.P. after multiplying
  // by any integer M
  static bool isAP(int A, int B, int C)
  {
    int new_b = B * 2;
 
    // Check if A, B, C are already
    // in A.P.
    if (new_b == (A + C))
      return true;
 
    // Check if multiplying B will
    // give A.P.
    if ((A + C) % new_b == 0)
      return true;
 
    if ((A + C) > new_b)
      return false;
 
    // Check if multiplying A will
    // give A.P.
    if ((new_b - C) % A == 0)
      return true;
 
    // Check if multiplying C will
    // give A.P.
    if ((new_b - A) % C == 0)
      return true;
 
    return false;
  }
 
  // Driver code
  public static int Main()
  {
    int A, B, C;
    A = 30;
    B = 5;
    C = 10;
 
    // Function call
    bool ans = isAP(A, B, C);
 
    // Displaying the answer
    if (ans)
      Console.Write("YES");
    else
      Console.Write("NO");
    return 0;
  }
}
 
// This code is contributed by Taranpreet


Javascript



输出
YES

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