给定两个数字A和B ,任务是找到使A和B相等所需的最小操作数。在每次操作中,任何数量的可以由2,3或7被划分。如果不可能,则打印-1 。
例子:
Input: A = 14, B = 28
Output: 1
Operation 1: A * 2 = 14 * 2 = 28 which is equal to B.
Input: A = 3, B = 5
Output: -1
No matter how many times the operation is performed, A and B will never be equal.
方法:A和B可被写为A = X * 2 A1 * 3 A2 * 7 a3和B = Y * 2 B1 * 3 B2 * 7 B3其中x和y不被2整除,3和7。现在,
- 如果x!= y,则不能使A和B等于给定的运算。
- 如果x = y,那么所需的最小运算将为| a1 – b1 | | + | a2 – b2 | + | a3 – b3 |因为这两个数字需要有2个,3个和7个相等的权力。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find powers of 2, 3 and 7 in x
vector Divisors(int x)
{
// To keep count of each divisor
int c = 0;
// To store the result
vector v;
// Count powers of 2 in x
while (x % 2 == 0) {
c++;
x /= 2;
}
v.push_back(c);
c = 0;
// Count powers of 3 in x
while (x % 3 == 0) {
c++;
x /= 3;
}
v.push_back(c);
c = 0;
// Count powers of 7 in x
while (x % 7 == 0) {
c++;
x /= 7;
}
v.push_back(c);
// Reamining number which is not
// divisible by 2, 3 or 7
v.push_back(x);
return v;
}
// Function to return the minimum number of
// given operations required to make a and b equal
int MinOperations(int a, int b)
{
// a = x * 2^a1 * 3^a2 * 7^a3
// va[0] = a1
// va[1] = a2
// va[2] = a3
// va[3] = x
vector va = Divisors(a);
// Similarly for b
vector vb = Divisors(b);
// If a and b cannot be made equal
// with the given operation. Note
// that va[3] and vb[3] contain
// remaining numbers after repeated
// divisions with 2, 3 and 7.
// If remaining numbers are not same
// then we cannot make them equal.
if (va[3] != vb[3])
return -1;
// Minimum number of operations required
int minOperations = abs(va[0] - vb[0])
+ abs(va[1] - vb[1])
+ abs(va[2] - vb[2]);
return minOperations;
}
// Driver code
int main()
{
int a = 14, b = 28;
cout << MinOperations(a, b);
return 0;
}
Java
// Java implementation of above approach
import java.util.Vector;
class GFG
{
// Function to find powers of 2, 3 and 7 in x
static Vector Divisors(int x)
{
// To keep count of each divisor
int c = 0;
// To store the result
Vector v = new Vector();
// Count powers of 2 in x
while (x % 2 == 0)
{
c++;
x /= 2;
}
v.add(c);
c = 0;
// Count powers of 3 in x
while (x % 3 == 0)
{
c++;
x /= 3;
}
v.add(c);
c = 0;
// Count powers of 7 in x
while (x % 7 == 0)
{
c++;
x /= 7;
}
v.add(c);
// Reamining number which is not
// divisible by 2, 3 or 7
v.add(x);
return v;
}
// Function to return the minimum number of
// given operations required to make a and b equal
static int MinOperations(int a, int b)
{
// a = x * 2^a1 * 3^a2 * 7^a3
// va[0] = a1
// va[1] = a2
// va[2] = a3
// va[3] = x
Vector va = Divisors(a);
// Similarly for b
Vector vb = Divisors(b);
// If a and b cannot be made equal
// with the given operation. Note
// that va[3] and vb[3] contain
// remaining numbers after repeated
// divisions with 2, 3 and 7.
// If remaining numbers are not same
// then we cannot make them equal.
if (va.get(3) != vb.get(3))
{
return -1;
}
// Minimum number of operations required
int minOperations = Math.abs(va.get(0) - vb.get(0))
+ Math.abs(va.get(1) - vb.get(1))
+ Math.abs(va.get(2) - vb.get(2));
return minOperations;
}
// Driver code
public static void main(String[] args)
{
int a = 14, b = 28;
System.out.println(MinOperations(a, b));
}
}
// This code is contributed by Rajput-JI
Python3
# python 3 implementation of the approach
# Function to find powers of 2, 3 and 7 in x
def Divisors(x):
# To keep count of each divisor
c = 0
# To store the result
v = []
# Count powers of 2 in x
while (x % 2 == 0):
c += 1
x /= 2
v.append(c)
c = 0
# Count powers of 3 in x
while (x % 3 == 0):
c += 1
x /= 3
v.append(c)
c = 0
# Count powers of 7 in x
while (x % 7 == 0):
c += 1
x /= 7
v.append(c)
# Reamining number which is not
# divisible by 2, 3 or 7
v.append(x)
return v
# Function to return the minimum number of
# given operations required to make a and b equal
def MinOperations(a, b):
# a = x * 2^a1 * 3^a2 * 7^a3
# va[0] = a1
# va[1] = a2
# va[2] = a3
# va[3] = x
va = Divisors(a)
# Similarly for b
vb = Divisors(b)
# If a and b cannot be made equal
# with the given operation. Note
# that va[3] and vb[3] contain
# remaining numbers after repeated
# divisions with 2, 3 and 7.
# If remaining numbers are not same
# then we cannot make them equal.
if (va[3] != vb[3]):
return -1
# Minimum number of operations required
minOperations = abs(va[0] - vb[0]) + abs(va[1] - vb[1]) + abs(va[2] - vb[2])
return minOperations
# Driver code
if __name__ == '__main__':
a = 14
b = 28
print(MinOperations(a, b))
# This code is contributed by
# Sanjit_Prasad
C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find powers
// of 2, 3 and 7 in x
static List Divisors(int x)
{
// To keep count of each divisor
int c = 0;
// To store the result
List v = new List();
// Count powers of 2 in x
while (x % 2 == 0)
{
c++;
x /= 2;
}
v.Add(c);
c = 0;
// Count powers of 3 in x
while (x % 3 == 0)
{
c++;
x /= 3;
}
v.Add(c);
c = 0;
// Count powers of 7 in x
while (x % 7 == 0)
{
c++;
x /= 7;
}
v.Add(c);
// Reamining number which is not
// divisible by 2, 3 or 7
v.Add(x);
return v;
}
// Function to return the minimum
// number of given operations required
// to make a and b equal
static int MinOperations(int a, int b)
{
// a = x * 2^a1 * 3^a2 * 7^a3
// va[0] = a1
// va[1] = a2
// va[2] = a3
// va[3] = x
List va = Divisors(a);
// Similarly for b
List vb = Divisors(b);
// If a and b cannot be made equal
// with the given operation. Note
// that va[3] and vb[3] contain
// remaining numbers after repeated
// divisions with 2, 3 and 7.
// If remaining numbers are not same
// then we cannot make them equal.
if (va[3] != vb[3])
{
return -1;
}
// Minimum number of operations required
int minOperations = Math.Abs(va[0] - vb[0]) +
Math.Abs(va[1] - vb[1]) +
Math.Abs(va[2] - vb[2]);
return minOperations;
}
// Driver code
public static void Main(String[] args)
{
int a = 14, b = 28;
Console.WriteLine(MinOperations(a, b));
}
}
// This code is contributed by
// PrinciRaj1992
PHP
Javascript
输出:
1