📌  相关文章
📜  如果任何一个被 M 整除,求 M 中的数字 A、B、C 构成 AP

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

如果任何一个被 M 整除,求 M 中的数字 A、B、C 构成 AP

给定三个正整数ABC ,任务是找出,如果我们将其中任何一个除以任何整数M(m>0 ),它们能否以相同的给定顺序形成 AP(Arithmetic Progression) .如果可能有多个值,则打印所有值,如果没有值,则打印 -1。

例子:

方法:

三个数字 A、B 和 C 在 AP 中,如果-

使用上述 AP 的公差性质,可以有三个公式用于三种情况-

  • A除以整数 m1-
  • B除以整数 m2-
  • C除以整数 m3-
  • 现在我们对每种情况都有可能的M值,然后检查三个值中的每一个,如果其中任何一个是正整数,那么该值就是所需的答案。
  • 如果不存在这样的可能值,则打印 -1。

以下是上述方法的实现 -

C++
// C++ code to implement the given approach
#include 
using namespace std;
 
// Utility function to check
// if given argument is an integer or not
bool ifint(double x)
{
    int a = x;
 
    if (x - a > 0)
        return false;
    else
        return true;
}
 
// Function to find any integer M if exists
void findVal(int A, int B, int C)
{
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1))
        cout << m1;
 
    else if (m2 > 1 && ifint(m2))
        cout << m2;
 
    else if (m3 > 1 && ifint(m3))
        cout << m3;
 
    else
        cout << "-1";
}
 
// Driver code
int main()
{
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Utility function to check
  // if given argument is an integer or not
  static Boolean ifint(double x)
  {
    int a = (int)x;
 
    if (x - a > 0)
      return false;
    else
      return true;
  }
 
  // Function to find any integer M if exists
  static void findVal(int A, int B, int C)
  {
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1)){
      int M1 = (int)m1;
      System.out.print(M1);
    }
 
    else if (m2 > 1 && ifint(m2)){
      int M2 = (int)m2;
      System.out.print(M2);
    }
 
    else if (m3 > 1 && ifint(m3)){
      int M3 = (int)m3;
      System.out.print(M3);
    }
 
    else
      System.out.print("-1");
  }
 
  // Driver code
  public static void main (String[] args) {
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);   
  }
}
 
// This code is contributed by hrithikgarg03188/


Python
# Python code to implement the given approach
 
# Utility function to check
# if given argument is an integer or not
def ifint(x):
    a = x
 
    if (x - a > 0):
        return False
    else:
        return True
 
# Function to find any integer M if exists
def findVal(A, B, C):
     
    m1 = (A / (2 * B - C))
    m2 = (2 * B / (C + A))
    m3 = (C / (2 * B - A));
 
    # Checks if it is both
    # positive and an integer
    if (m1 > 1 and ifint(m1)):
        print(m1)
 
    elif (m2 > 1 and ifint(m2)):
        print(m2)
 
    elif (m3 > 1 and ifint(m3)):
        print(m3)
 
    else:
        print(-1)
 
# Driver code
A = 2
B = 4
C = 18
 
findVal(A, B, C)
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# code to implement the given approach
using System;
class GFG {
    // Utility function to check
    // if given argument is an integer or not
    static bool ifint(double x)
    {
        double a = x;
 
        if (x - a > 0)
            return false;
        else
            return true;
    }
 
    // Function to find any integer M if exists
    static void findVal(int A, int B, int C)
    {
        double m1 = Convert.ToDouble(A / (2 * B - C));
        double m2 = Convert.ToDouble(2 * B / (C + A));
        double m3 = Convert.ToDouble(C / (2 * B - A));
 
        // Checks if it is both
        // positive and an integer
        if (m1 > 1 && ifint(m1))
            Console.Write(m1);
 
        else if (m2 > 1 && ifint(m2))
            Console.Write(m2);
 
        else if (m3 > 1 && ifint(m3))
            Console.Write(m3);
 
        else
            Console.Write(-1);
    }
 
    // Driver code
    public static int Main()
    {
        int A = 2;
        int B = 4;
        int C = 18;
 
        findVal(A, B, C);
        return 0;
    }
}
 
// This code is contributed by Taranpreet


Javascript


C++
// C++ code to implement the given approach
#include 
using namespace std;
 
// Utility function to check
// if given argument is an integer or not
bool ifint(double x)
{
    int a = x;
 
    if (x - a > 0)
        return false;
    else
        return true;
}
 
// Function to find any integer M if exists
void findVal(int A, int B, int C)
{
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1))
        cout << m1;
 
    else if (m2 > 1 && ifint(m2))
        cout << m2;
 
    else if (m3 > 1 && ifint(m3))
        cout << m3;
 
    else
        cout << "-1";
}
 
// Driver code
int main()
{
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Utility function to check
  // if given argument is an integer or not
  static Boolean ifint(double x)
  {
    int a = (int)x;
 
    if (x - a > 0)
      return false;
    else
      return true;
  }
 
  // Function to find any integer M if exists
  static void findVal(int A, int B, int C)
  {
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1)){
      int M1 = (int)m1;
      System.out.print(M1);
    }
 
    else if (m2 > 1 && ifint(m2)){
      int M2 = (int)m2;
      System.out.print(M2);
    }
 
    else if (m3 > 1 && ifint(m3)){
      int M3 = (int)m3;
      System.out.print(M3);
    }
 
    else
      System.out.print("-1");
  }
 
  // Driver code
  public static void main (String[] args) {
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);   
  }
}
 
// This code is contributed by hrithikgarg03188/


Python3
# Python code for the above approach
 
# Utility function to check
# if given argument is an integer or not
def ifint(x):
    a = x;
 
    if (x - a > 0):
        return False;
    else:
        return True;
 
# Function to find any integer M if exists
def findVal(A, B, C):
    m1 = (A / (2 * B - C));
    m2 = (2 * B / (C + A));
    m3 = (C / (2 * B - A));
 
    # Checks if it is both
    # positive and an integer
    if (m1 > 1 and ifint(m1)):
        print(int(m1))
 
    elif (m2 > 1 and ifint(m2)):
        print(int(m2))
 
    elif (m3 > 1 and ifint(m3)):
        print(int(m3))
 
    else:
        print("-1");
 
# Driver code
A = 2;
B = 4;
C = 18;
 
findVal(A, B, C);
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code to implement the given approach
using System;
class GFG
{
   
    // Utility function to check
    // if given argument is an integer or not
    static bool ifint(double x)
    {
        double a = x;
 
        if (x - a > 0)
            return false;
        else
            return true;
    }
 
    // Function to find any integer M if exists
    static void findVal(int A, int B, int C)
    {
        double m1 = Convert.ToDouble(A / (2 * B - C));
        double m2 = Convert.ToDouble(2 * B / (C + A));
        double m3 = Convert.ToDouble(C / (2 * B - A));
 
        // Checks if it is both
        // positive and an integer
        if (m1 > 1 && ifint(m1))
            Console.Write(m1);
 
        else if (m2 > 1 && ifint(m2))
            Console.Write(m2);
 
        else if (m3 > 1 && ifint(m3))
            Console.Write(m3);
 
        else
            Console.Write(-1);
    }
 
    // Driver code
    public static int Main()
    {
        int A = 2;
        int B = 4;
        int C = 18;
 
        findVal(A, B, C);
        return 0;
    }
}
 
// This code is contributed by Taranpreet


Javascript



输出
3

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

C++

// C++ code to implement the given approach
#include 
using namespace std;
 
// Utility function to check
// if given argument is an integer or not
bool ifint(double x)
{
    int a = x;
 
    if (x - a > 0)
        return false;
    else
        return true;
}
 
// Function to find any integer M if exists
void findVal(int A, int B, int C)
{
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1))
        cout << m1;
 
    else if (m2 > 1 && ifint(m2))
        cout << m2;
 
    else if (m3 > 1 && ifint(m3))
        cout << m3;
 
    else
        cout << "-1";
}
 
// Driver code
int main()
{
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Utility function to check
  // if given argument is an integer or not
  static Boolean ifint(double x)
  {
    int a = (int)x;
 
    if (x - a > 0)
      return false;
    else
      return true;
  }
 
  // Function to find any integer M if exists
  static void findVal(int A, int B, int C)
  {
    double m1 = (double)(A / (2 * B - C));
    double m2 = (double)(2 * B / (C + A));
    double m3 = (double)(C / (2 * B - A));
 
    // Checks if it is both
    // positive and an integer
    if (m1 > 1 && ifint(m1)){
      int M1 = (int)m1;
      System.out.print(M1);
    }
 
    else if (m2 > 1 && ifint(m2)){
      int M2 = (int)m2;
      System.out.print(M2);
    }
 
    else if (m3 > 1 && ifint(m3)){
      int M3 = (int)m3;
      System.out.print(M3);
    }
 
    else
      System.out.print("-1");
  }
 
  // Driver code
  public static void main (String[] args) {
    int A = 2;
    int B = 4;
    int C = 18;
 
    findVal(A, B, C);   
  }
}
 
// This code is contributed by hrithikgarg03188/

Python3

# Python code for the above approach
 
# Utility function to check
# if given argument is an integer or not
def ifint(x):
    a = x;
 
    if (x - a > 0):
        return False;
    else:
        return True;
 
# Function to find any integer M if exists
def findVal(A, B, C):
    m1 = (A / (2 * B - C));
    m2 = (2 * B / (C + A));
    m3 = (C / (2 * B - A));
 
    # Checks if it is both
    # positive and an integer
    if (m1 > 1 and ifint(m1)):
        print(int(m1))
 
    elif (m2 > 1 and ifint(m2)):
        print(int(m2))
 
    elif (m3 > 1 and ifint(m3)):
        print(int(m3))
 
    else:
        print("-1");
 
# Driver code
A = 2;
B = 4;
C = 18;
 
findVal(A, B, C);
 
# This code is contributed by Saurabh Jaiswal

C#

// C# code to implement the given approach
using System;
class GFG
{
   
    // Utility function to check
    // if given argument is an integer or not
    static bool ifint(double x)
    {
        double a = x;
 
        if (x - a > 0)
            return false;
        else
            return true;
    }
 
    // Function to find any integer M if exists
    static void findVal(int A, int B, int C)
    {
        double m1 = Convert.ToDouble(A / (2 * B - C));
        double m2 = Convert.ToDouble(2 * B / (C + A));
        double m3 = Convert.ToDouble(C / (2 * B - A));
 
        // Checks if it is both
        // positive and an integer
        if (m1 > 1 && ifint(m1))
            Console.Write(m1);
 
        else if (m2 > 1 && ifint(m2))
            Console.Write(m2);
 
        else if (m3 > 1 && ifint(m3))
            Console.Write(m3);
 
        else
            Console.Write(-1);
    }
 
    // Driver code
    public static int Main()
    {
        int A = 2;
        int B = 4;
        int C = 18;
 
        findVal(A, B, C);
        return 0;
    }
}
 
// This code is contributed by Taranpreet

Javascript