📌  相关文章
📜  最小化将所有数组元素转换为斐波那契数的成本

📅  最后修改于: 2021-09-04 11:24:57             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是最小化将所有数组元素转换为斐波那契数的成本,其中将数字A转换为B的成本是AB之间的绝对差。

例子:

方法:给定的问题可以通过将每个数组元素替换为其最近的斐波那契数来解决,以获得将所有数组元素更改为斐波那契数的最小成本。请按照以下步骤解决给定的问题:

  • 初始化变量比如成本,存储改变所有数组元素Fibonacci数的最小成本。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 要找到最接近arr[i]的斐波那契数,首先,使用公式找到N的值,使得N斐波那契数arr[i] N = \frac {\log(\sqrt 5*arr[i])}{\log \frac{1 + \sqrt 5}{2}}
    • 现在,找到第N(N + 1)斐波那契数,分别假设为XY ,并将(X – arr[i])(Y – arr[i])的绝对值的最小值添加到成本中
  • 完成上述步骤后,打印成本值作为结果的最小成本。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the
// N-th Fibonacci Number
int nthFibo(int n)
{
     
    // Find the value of a, b, and r
    double a = (pow(5, 0.5) + 1) / 2;
    double b = (-1*(pow(5, 0.5) ) + 1) / 2;
    double r = pow(5, 0.5);
 
    // Find the N-th Fibonacci
    double ans = (pow(a, n) - pow(b, n)) / r;
 
    // Return the result
    return int(ans);
}
 
// Function to find the Fibonacci
// number which is nearest to X
int nearFibo(int X)
{
    double a = (pow(5, 0.5) + 1) / 2;
 
    // Calculate the value of n for X
    int n = int(log((pow(5, 0.5)) * X) / log(a));
 
    int nth = nthFibo(n);
    int nplus = nthFibo(n + 1);
 
    // Return the nearest
    // Fibonacci Number
    if (abs(X - nth) < abs(X - nplus))
        return nth;
    else
        return nplus;
}
 
// Function to find the minimum
// cost to conevert all array
// elements to Fibonacci Numbers
int getCost(int arr[], int n)
{
 
    // Stores the total minimum cost
    int cost = 0;
 
    // Traverse the given array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Find the nearest
        // Fibonacci Number
        int fibo = nearFibo(arr[i]);
 
        // Add the cost
        cost += abs(arr[i] - fibo);
    }
     
    // Return the final cost
    return cost;
}
 
// Driver Code
int main()
{
    int arr[] = { 56, 34, 23, 98, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << (getCost(arr, n));
}
 
// This code is contributed by ukasp


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG
{
   
// Function to find the
// N-th Fibonacci Number
static int nthFibo(int n)
{
     
    // Find the value of a, b, and r
    double a = (Math.pow(5, 0.5) + 1) / 2;
    double b = (-1*(Math.pow(5, 0.5) ) + 1) / 2;
    double r = Math.pow(5, 0.5);
 
    // Find the N-th Fibonacci
    double ans = (Math.pow(a, n) - Math.pow(b, n)) / r;
 
    // Return the result
    return (int)ans;
}
 
// Function to find the Fibonacci
// number which is nearest to X
static int nearFibo(int X)
{
    double a = (Math.pow(5, 0.5) + 1) / 2;
 
    // Calculate the value of n for X
    int n = (int)(Math.log((Math.pow(5, 0.5)) * X) / Math.log(a));
 
    int nth = nthFibo(n);
    int nplus = nthFibo(n + 1);
 
    // Return the nearest
    // Fibonacci Number
    if (Math.abs(X - nth) < Math.abs(X - nplus))
        return nth;
    else
        return nplus;
}
 
// Function to find the minimum
// cost to conevert all array
// elements to Fibonacci Numbers
static int getCost(int arr[], int n)
{
 
    // Stores the total minimum cost
    int cost = 0;
 
    // Traverse the given array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Find the nearest
        // Fibonacci Number
        int fibo = nearFibo(arr[i]);
 
        // Add the cost
        cost += Math.abs(arr[i] - fibo);
    }
     
    // Return the final cost
    return cost;
}
 
// Driver code
public static void main (String[] args)
{
 int arr[] = { 56, 34, 23, 98, 7 };
    int n = arr.length;
 System.out.print(getCost(arr, n));
    }
}
 
// This code is contributed by offbeat


Python3
# Python program for the above approach
 
import math
 
# Function to find the
# N-th Fibonacci Number
def nthFibo(n):
   
    # Find the value of a, b, and r
    a = (5**(1 / 2) +1)/2
    b = (-5**(1 / 2) +1)/2
    r = 5**(1 / 2)
 
 
    # Find the N-th Fibonacci
    ans = (a**n - b**n)/r
 
    # Return the result
    return int(ans)
 
# Function to find the Fibonacci
# number which is nearest to X
def nearFibo(X):
   
    a = (5**(1 / 2)+1)/2
     
    # Calculate the value of n for X
    n = int(math.log((5**(1 / 2))*X) / math.log(a))
 
    nth = nthFibo(n)
    nplus = nthFibo(n + 1)
 
    # Return the nearest
    # Fibonacci Number
    if abs(X - nth) < abs(X - nplus):
        return nth
    else:
        return nplus
 
# Function to find the minimum
# cost to conevert all array
# elements to Fibonacci Numbers
def getCost(arr):
   
    # Stores the total minimum cost
    cost = 0
     
    # Traverse the given array arr[]
    for i in arr:
       
        # Find the nearest
        # Fibonacci Number
        fibo = nearFibo(i)
         
        # Add the cost
        cost += abs(i-fibo)
 
    # Return the final cost
    return cost
 
# Driver Code
 
arr = [56, 34, 23, 98, 7]
 
print(getCost(arr))


C#
// C# program to count frequencies of array items
using System;
 
class GFG{
     
// Function to find the
// N-th Fibonacci Number
static int nthFibo(int n)
{
     
    // Find the value of a, b, and r
    double a = (Math.Pow(5, 0.5) + 1) / 2;
    double b = (-1*(Math.Pow(5, 0.5) ) + 1) / 2;
    double r = Math.Pow(5, 0.5);
 
    // Find the N-th Fibonacci
    double ans = (Math.Pow(a, n) -
                  Math.Pow(b, n)) / r;
 
    // Return the result
    return (int)ans;
}
 
// Function to find the Fibonacci
// number which is nearest to X
static int nearFibo(int X)
{
    double a = (Math.Pow(5, 0.5) + 1) / 2;
 
    // Calculate the value of n for X
    int n = (int)(Math.Log((Math.Pow(5, 0.5)) * X) /
                            Math.Log(a));
 
    int nth = nthFibo(n);
    int nplus = nthFibo(n + 1);
 
    // Return the nearest
    // Fibonacci Number
    if (Math.Abs(X - nth) < Math.Abs(X - nplus))
        return nth;
    else
        return nplus;
}
 
// Function to find the minimum
// cost to conevert all array
// elements to Fibonacci Numbers
static int getCost(int []arr, int n)
{
 
    // Stores the total minimum cost
    int cost = 0;
 
    // Traverse the given array arr[]
    for(int i = 0; i < n; i++)
    {
         
        // Find the nearest
        // Fibonacci Number
        int fibo = nearFibo(arr[i]);
 
        // Add the cost
        cost += Math.Abs(arr[i] - fibo);
    }
     
    // Return the final cost
    return cost;
}   
 
// Driver code
public static void Main(String []args)
{
    int []arr = { 56, 34, 23, 98, 7 };
    int n = arr.Length;
     
    Console.WriteLine(getCost(arr, n));
}
}
 
// This code is contributed by jana_sayantan


Javascript


输出:
13

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

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