如果任何一个被 M 整除,求 M 中的数字 A、B、C 构成 AP
给定三个正整数A 、 B和C ,任务是找出,如果我们将其中任何一个除以任何整数M(m>0 ),它们能否以相同的给定顺序形成 AP(Arithmetic Progression) .如果可能有多个值,则打印所有值,如果没有值,则打印 -1。
例子:
Input: A = 25, B = 10, C = 15
Output: 5
Explanation: If A(25) is divided by 5 then the three integers are 5, 10, 15 which form an A.P
with common difference 5.
Input: A = 18, B = 4, C = 2
Output: 3.
Explanation: If A(18) is divided by 3 then the three integers are 6, 4, 2 which form an A.P
with common difference -2.
Input: A = 7, B = 11, C = 13
Output: -1
Explanation: It can be proved that their exists no positive integer which on dividing with
any one of the three numbers can form an A.P.
方法:
三个数字 A、B 和 C 在 AP 中,如果-
B – A = C – B = d
Here,
A, B, and C are the three numbers
d is the common difference
使用上述 AP 的公差性质,可以有三个公式用于三种情况-
- 当A除以整数 m1-
For A / m1, B, C to form an A.P., we have
B – A / m1 = C – B
Thus, m1 = a / (2 * B – C)
- 当B除以整数 m2-
For A, B / m2, C to form an A.P., we have
B / m2 – A = C – B / m2
Thus, m2 = 2 * B/ (C + A)
- 当C除以整数 m3-
For A, B, C / m3 to form an A.P., we have
B – A = C / m3 – B
Thus, m3 = C / (2 * B – A)
- 现在我们对每种情况都有可能的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