📜  总和等于 K 的最小斐波那契项

📅  最后修改于: 2021-10-27 03:21:11             🧑  作者: Mango

给定一个数 k,找出总和等于 k 的所需的最小斐波那契项数。我们可以多次选择一个斐波那契数。

例子:

Input : k = 4
Output : 2
Fibonacci term added twice that is
2 + 2 = 4. 
Other combinations are 
1 + 1 + 2. 
1 + 1 + 1 + 1
Among all cases first case has the 
minimum number of terms = 2.

Input : k = 17
Output : 3
推荐:请先在 IDE 上尝试您的方法,然后再查看解决方案

我们可以使用斐波那契数获得任何和,因为 1 是斐波那契数。例如,要得到 n,我们可以 n 次加 1。这里我们需要最小化对总和有贡献的斐波那契数的数量。所以这个问题基本上是具有斐波那契值的硬币的硬币找零问题。通过举一些例子,我们可以注意到使用斐波那契硬币价值贪婪方法是有效的。
首先我们计算斐波那契项直到小于或等于 k。然后从最后一项开始,不断地从 k 中减去那个项,直到 k >(nth term)。与此同时,不断增加术语的数量。

当 k <(第 n 个斐波那契项)移动到下一个小于或等于 k 的斐波那契项时。最后,打印count的值。
逐步算法是:

1. Find all Fibonacci Terms less than or equal to K.
  2. Initialize count = 0.
  3. j = Index of last calculated Fibonacci Term.
  4. while K > 0 do:
    
      // Greedy step
      count += K / (fibo[j])     // Note that division 
                                 // is repeated subtraction.
      K = K % (fibo[j])
      j--;
  5. Print count.

下面是上述方法的实现。

C++
// C++ code to find minimum number of fibonacci
// terms that sum to K.
#include 
using namespace std;
 
// Function to calculate Fibonacci Terms
void calcFiboTerms(vector& fiboTerms, int K)
{
    int i = 3, nextTerm;
    fiboTerms.push_back(0);
    fiboTerms.push_back(1);
    fiboTerms.push_back(1);
     
    // Calculate all Fibonacci terms
    // which are less than or equal to K.
    while (1) {
        nextTerm = fiboTerms[i - 1] + fiboTerms[i - 2];
 
        // If next term is greater than K
        // do not push it in vector and return.
        if (nextTerm > K)
            return;
 
        fiboTerms.push_back(nextTerm);
        i++;
    }
}
 
// Function to find the minimum number of
// Fibonacci terms having sum equal to K.
int findMinTerms(int K)
{
 
    // Vector to store Fibonacci terms.
    vector fiboTerms;
 
    calcFiboTerms(fiboTerms, K);
 
    int count = 0, j = fiboTerms.size() - 1;
 
    // Subtract Fibonacci terms from sum K
    // until sum > 0.
    while (K > 0) {
         
        // Divide sum K by j-th Fibonacci term to find
        // how many terms it contribute in sum.
        count += (K / fiboTerms[j]);
        K %= (fiboTerms[j]);
 
        j--;
    }
 
    return count;
}
 
// driver code
int main()
{
    int K = 17;
 
    cout << findMinTerms(K);
 
    return 0;
}


Java
// Java code to find the minimum number of Fibonacci terms
// that sum to k.
import java.util.*;
 
class GFG
{
    // Function to calaculate Fibonacci Terms
    public static void calcFiboTerms(ArrayList fiboterms,
                                                            int k)
    {
        int i = 3, nextTerm = 0;
             
        fiboterms.add(0);
        fiboterms.add(1);
        fiboterms.add(1);
             
        // Calculate all Fibonacci terms
        // which are less than or equal to k.
        while(true)
        {
            nextTerm = fiboterms.get(i - 1) + fiboterms.get(i - 2);
                 
            // If next term is greater than k
            // do not add in arraylist and return.
            if(nextTerm>k)
            return;
                 
            fiboterms.add(nextTerm);
            i++;
        }
    }
     
    // Function to find the minimum number of
    // Fibonacci terms having sum equal to k.
    public static int fibMinTerms(int k)
    {
        // ArrayList to store Fibonacci terms.
        ArrayList fiboterms = new ArrayList();
        calcFiboTerms(fiboterms,k);
         
        int count = 0, j = fiboterms.size() - 1;
         
        // Subtract Fibonacci terms from sum k
        // until sum > 0.
        while(k > 0)
        {
            // Divide sum k by j-th Fibonacci term to find
            // how many terms it contribute in sum.
            count += (k / fiboterms.get(j));
            k %= (fiboterms.get(j));
             
            j--;
        }
        return count;
    }
     
    // driver code
    public static void main (String[] args) {
     
        int k = 17;
     
        System.out.println(fibMinTerms(k));
    }
}
 
/* This code is contributed by Akash Singh*/


Python3
# Python3 code to find minimum number
# of Fibonacci terms that sum to K.
 
# Function to calculate Fibonacci Terms
def calcFiboTerms(fiboTerms, K):
 
    i = 3
    fiboTerms.append(0)
    fiboTerms.append(1)
    fiboTerms.append(1)
     
    # Calculate all Fibonacci terms
    # which are less than or equal to K.
    while True:
        nextTerm = (fiboTerms[i - 1] +
                    fiboTerms[i - 2])
 
        # If next term is greater than K
        # do not push it in vector and return.
        if nextTerm > K:
            return
 
        fiboTerms.append(nextTerm)
        i += 1
     
# Function to find the minimum number of
# Fibonacci terms having sum equal to K.
def findMinTerms(K):
 
    # Vector to store Fibonacci terms.
    fiboTerms = []
    calcFiboTerms(fiboTerms, K)
 
    count, j = 0, len(fiboTerms) - 1
 
    # Subtract Fibonacci terms from
    # sum K until sum > 0.
    while K > 0:
         
        # Divide sum K by j-th Fibonacci
        # term to find how many terms it
        # contribute in sum.
        count += K // fiboTerms[j]
        K %= fiboTerms[j]
 
        j -= 1
     
    return count
 
# Driver code
if __name__ == "__main__":
 
    K = 17
    print(findMinTerms(K))
 
# This code is contributed
# by Rituraj Jain


C#
// C# code to find the minimum number
// of Fibonacci terms that sum to k.
using System;
using System.Collections.Generic;
     
class GFG
{
// Function to calaculate Fibonacci Terms
public static void calcFiboTerms(List fiboterms,
                                      int k)
{
    int i = 3, nextTerm = 0;
         
    fiboterms.Add(0);
    fiboterms.Add(1);
    fiboterms.Add(1);
         
    // Calculate all Fibonacci terms
    // which are less than or equal to k.
    while(true)
    {
        nextTerm = fiboterms[i - 1] +
                   fiboterms[i - 2];
             
        // If next term is greater than k
        // do not add in arraylist and return.
        if(nextTerm > k)
            return;
             
        fiboterms.Add(nextTerm);
        i++;
    }
}
 
// Function to find the minimum number of
// Fibonacci terms having sum equal to k.
public static int fibMinTerms(int k)
{
    // List to store Fibonacci terms.
    List fiboterms = new List();
    calcFiboTerms(fiboterms, k);
     
    int count = 0, j = fiboterms.Count - 1;
     
    // Subtract Fibonacci terms from sum k
    // until sum > 0.
    while(k > 0)
    {
        // Divide sum k by j-th Fibonacci term to find
        // how many terms it contribute in sum.
        count += (k / fiboterms[j]);
        k %= (fiboterms[j]);
         
        j--;
    }
    return count;
}
 
// Driver Code
public static void Main (String[] args)
{
    int k = 17;
 
    Console.WriteLine(fibMinTerms(k));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程