给定三个整数a , b和x ,任务是获得x的最接近a b的倍数。
例子:
Input: a = 5, b = 4, x = 3
Output: 624
54 = 625 and 624 is the multiple of 3 which is closest to 625
Input: a = 349, b = 1, x = 4
Output: 348
方法:
- 计算b并将其存储在变量num中。
- 然后,计算⌊num/x⌋并将其存储在变量floor中。
- 现在,左侧最接近的元素将是closetLeft = x * floor 。
- 右边最接近的元素将是closetRight = x *(floor + 1) 。
- 最后,其中最接近的数字将是min(num-最近的左,最近的正确-num) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long int
// Function to return the multiple of x
// which is closest to a^b
ll getClosest(int a, int b, int x)
{
ll num = pow(a, b);
int floor = num / x;
// Closest element on the left
ll numOnLeft = x * floor;
// Closest element on the right
ll numOnRight = x * (floor + 1);
// If numOnLeft is closer than numOnRight
if ((num - numOnLeft) < (numOnRight - num))
return numOnLeft;
// If numOnRight is the closest
else
return numOnRight;
}
// Driver code
int main()
{
int a = 349, b = 1, x = 4;
cout << getClosest(a, b, x) << endl;
return 0;
}
Java
//Java implementation of the approach
public class GFG {
// Function to return the multiple of x
// which is closest to a^b
static long getClosest(int a, int b, int x) {
long num = (long) Math.pow(a, b);
int floor = (int) (num / x);
// Closest element on the left
long numOnLeft = x * floor;
// Closest element on the right
long numOnRight = x * (floor + 1);
// If numOnLeft is closer than numOnRight
if ((num - numOnLeft) < (numOnRight - num)) {
return numOnLeft;
} // If numOnRight is the closest
else {
return numOnRight;
}
}
public static void main(String[] args) {
int a = 349, b = 1, x = 4;
System.out.println(getClosest(a, b, x));
}
}
Python3
# Python3 implementation of the approach
# Function to return the multiple of x
# which is closest to a^b
def getClosest(a, b, x) :
num = pow(a, b)
floor = num // x
# Closest element on the left
numOnLeft = x * floor
# Closest element on the right
numOnRight = x * (floor + 1)
# If numOnLeft is closer than numOnRight
if ((num - numOnLeft) <
(numOnRight - num)):
return numOnLeft
# If numOnRight is the closest
else :
return numOnRight
# Driver code
if __name__ == "__main__" :
a, b, x = 349, 1, 4
print(getClosest(a, b, x))
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
// #define ll long long int
class GFG
{
// Function to return the multiple of x
// which is closest to a^b
static long getClosest(int a, int b, int x)
{
int num = (int)Math.Pow(a, b);
int floor = (int)(num / x);
// Closest element on the left
int numOnLeft = (int)(x * floor);
// Closest element on the right
int numOnRight = (int)(x * (floor + 1));
// If numOnLeft is closer than numOnRight
if ((num - numOnLeft) < (numOnRight - num))
return numOnLeft;
// If numOnRight is the closest
else
return numOnRight;
}
// Driver code
public static void Main()
{
int a = 349, b = 1, x = 4;
Console.WriteLine(getClosest(a, b, x));
}
}
// This code is contributed
// by Akanksha Rai
PHP
输出:
348
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。