📌  相关文章
📜  用最接近的 K 次幂替换包含 K 作为数字的数组元素

📅  最后修改于: 2021-09-06 06:35:52             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ,任务是将包含K 的每个数组元素替换为一个数字,用其最近的K 次幂。
注意:如果碰巧有两个最近的幂,则取较大的一个。

例子:

方法:想法是将数组的每个元素转换为字符串,然后在字符串搜索K ,如果找到则将其替换为K的最近幂。请按照以下步骤解决问题:

  • 声明一个函数来寻找K的最近幂
    • P的值,其中X p 将最接近元素。
    • 为了计算plog X (Element) 的下限值。
    • 因此, pp+1将是可以获得最接近幂的两个整数。
    • 计算X kX (K + 1)并检查哪个更接近元素并返回该元素。
  • 遍历数组arr[]
    • 将每个元素转换为字符串。
    • 遍历字符串并检查数字K是否存在,如果找到,则将数组元素替换为最近的K 次幂并从该点中断。
  • 打印修改后的数组。

下面是上述方法的实现:

C++
// C++program  for the above approach
#include 
using namespace std;
 
// Function to calculate the
// power of base nearest to x
int nearestPow(int x, int base)
{
 
    // Stores logX to the base K
    int k = int(log(x) / log(base));
    if (abs(pow(base, k) - x) < abs(pow(base, (k + 1)) - x))
        return pow(base, k);
    else
        return pow(base, (k + 1));
}
 
// Function to replace array
// elements with nearest power of K
void replaceWithNearestPowerOfK(int arr[], int K, int n)
{
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Convert integer into a string
        string strEle = to_string(arr[i]);
       
 
        for (int c = 0; c < strEle.length(); c++) {
 
            // If K is found, then replace
            // with the nearest power of K
            if ((strEle-'0') == K) {
            
                arr[i] = nearestPow(arr[i], K);
                break;
            }
        }
    }
   
    // Print the array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
// Driver Code
int main()
{
   
    // Given array
    int arr[] = { 432, 953, 232, 333 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of K
    int K = 3;
 
    // Function call to replace array
    // elements with nearest power of K
    replaceWithNearestPowerOfK(arr, K, n);
}
 
// This code is contributed by ukasp.


Python3
# Python3 program
# for the above approach
import math
 
# Function to calculate the
# power of base nearest to x
def nearestPow(x, base):
   
    # Stores logX to the base K
    k = int(math.log(x, base))
 
    if abs(base**k - x) < abs(base**(k+1) - x):
        return base**k
    else:
        return base**(k+1)
 
# Function to replace array
# elements with nearest power of K
def replaceWithNearestPowerOfK(arr, K):
 
    # Traverse the array
    for i in range(len(arr)):
 
        # Convert integer into a string
        strEle = str(arr[i])
 
        for c in strEle:
 
            # If K is found, then replace
            # with the nearest power of K
            if int(c) == K:
                arr[i] = nearestPow(arr[i], K)
                break
           
    # Print the array
    print(arr)
 
# Driver Code
 
# Given array
arr = [432, 953, 232, 333]
 
# Given value of K
K = 3
 
# Function call to replace array
# elements with nearest power of K
replaceWithNearestPowerOfK(arr, K)


Javascript


C#
// C# program  for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate the
// power of base nearest to x
static int nearestPow(int x, int base1)
{
 
    // Stores logX to the base K
    int k = (int)(Math.Log(x) / Math.Log(base1));
    if (Math.Abs(Math.Pow(base1, k) - x) < Math.Abs(Math.Pow(base1, (k + 1)) - x))
        return (int)Math.Pow(base1, k);
    else
        return (int)Math.Pow(base1, (k + 1));
}
 
// Function to replace array
// elements with nearest power of K
static void replaceWithNearestPowerOfK(int []arr, int K, int n)
{
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Convert integer into a string
        int num = arr[i];
        string strEle = num.ToString();
 
        for (int c = 0; c < strEle.Length; c++) {
 
            // If K is found, then replace
            // with the nearest power of K
            if ((strEle-'0') == K) {
            
                arr[i] = nearestPow(arr[i], K);
                break;
            }
        }
    }
   
    // Print the array
    for (int i = 0; i < n; i++)
        Console.Write(arr[i]+" ");
}
// Driver Code
public static void Main()
{
   
    // Given array
    int []arr = { 432, 953, 232, 333 };
    int n = arr.Length;
 
    // Given value of K
    int K = 3;
 
    // Function call to replace array
    // elements with nearest power of K
    replaceWithNearestPowerOfK(arr, K, n);
}
}
 
// This code is contributed by ipg2016107.


输出:
[243, 729, 243, 243]

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live