📜  一对具有给定总和和最小绝对差的斐波那契数

📅  最后修改于: 2021-04-22 01:03:32             🧑  作者: Mango

给定一个整数N (小于10 ^ 6),任务是找到一对斐波那契数,其总和等于给定N ,所选对之间的绝对差最小。
如果没有解决方法,请打印-1
例子:

方法:想法是使用散列来预先计算和存储Fibonacci节点到最大值,以使检查变得容易和高效(在O(1)时间内)。
在预计算哈希之后:

  1. 从(N / 2)到1开始一个循环(以最小化绝对差),并检查循环计数器“ i”和“ N – i”是否均为斐波那契。
  2. 如果它们是斐波那契,那么我们将打印它们并退出循环。
  3. 如果数字N不能表示为两个斐波那契数字的总和,那么我们将打印-1。

下面是上述方法的实现:

C++
// C++ program to find the pair of
// Fibonacci numbers with a given sum
// and minimum absolute difference
 
#include 
using namespace std;
#define MAX 1000005
 
// Hash to store all the
// Fibonacci numbers
set fib;
 
// Function to generate fibonacci Series
// and create hash table
// to check Fibonacci numbers
void fibonacci()
{
    // Adding the first two Fibonacci
    // numbers into the Hash set
    int prev = 0, curr = 1, len = 2;
    fib.insert(prev);
    fib.insert(curr);
 
    // Computing the remaining Fibonacci
    // numbers based on the previous
    // two numbers
    while (len <= MAX) {
        int temp = curr + prev;
        fib.insert(temp);
        prev = curr;
        curr = temp;
        len++;
    }
}
 
// Function to find the pair of
// Fibonacci numbers with the given
// sum and minimum absolute difference
void findFibonacci(int N)
{
 
    // Start from N/2 such that the
    // difference between i and
    // N - i will be minimum
    for (int i = N / 2; i > 1; i--) {
 
        // If both 'i' and 'sum - i' are
        // fibonacci numbers then print
        // them and break the loop
        if (fib.find(i) != fib.end()
            && fib.find(N - i) != fib.end()) {
 
            cout << i << " " << (N - i) << endl;
            return;
        }
    }
 
    // If there is no Fibonacci pair
    // possible
    cout << "-1" << endl;
}
 
// Driver code
int main()
{
    // Generate the Fibonacci numbers
    fibonacci();
 
    int sum = 199;
 
    // Find the Fibonacci pair
    findFibonacci(sum);
 
    return 0;
}


Java
// Java program to find the pair of
// Fibonacci numbers with a given sum
// and minimum absolute difference
import java.util.*;
  
class GFG{
    // Hash to store all the
    // Fibonacci numbers
    static int MAX=1000005;
    static  Set fib=new HashSet();
     
    // Function to generate fibonacci Series
    // and create hash table
    // to check Fibonacci numbers
    static void fibonacci()
    {
        // Adding the first two Fibonacci
        // numbers into the Hash set
        int prev = 0, curr = 1, len = 2;
        fib.add(prev);
        fib.add(curr);
     
        // Computing the remaining Fibonacci
        // numbers based on the previous
        // two numbers
        while (len <= MAX) {
            int temp = curr + prev;
            fib.add(temp);
            prev = curr;
            curr = temp;
            len++;
        }
    }
     
    // Function to find the pair of
    // Fibonacci numbers with the given
    // sum and minimum absolute difference
    static void findFibonacci(int N)
    {
     
        // Start from N/2 such that the
        // difference between i and
        // N - i will be minimum
        for (int i = N / 2; i > 1; i--) {
     
            // If both 'i' and 'sum - i' are
            // fibonacci numbers then print
            // them and break the loop
            if (fib.contains(i)  && fib.contains(N - i)) {
     
                System.out.println(i+" "+(N - i));
                return;
            }
        }
     
        // If there is no Fibonacci pair
        // possible
        System.out.println("-1");
    }
     
    // Driver code
    public static void main(String args[])
    {
        // Generate the Fibonacci numbers
        fibonacci();
     
        int sum = 199;
     
        // Find the Fibonacci pair
        findFibonacci(sum);
     
         
    }
}
 
 
// This code is contributed by AbhiThakur


Python3
# Python 3 program to find
# the pair of Fibonacci numbers
# with a given sum and minimum
# absolute difference
MAX = 10005
 
# Hash to store all the
# Fibonacci numbers
fib = set()
 
# Function to generate
# fibonacci Series and
# create hash table
# to check Fibonacci
# numbers
def fibonacci():
   
    global fib
    global MAX
     
    # Adding the first
    # two Fibonacci numbers
    # into the Hash set
    prev = 0
    curr = 1
    l = 2
    fib.add(prev)
    fib.add(curr)
 
    # Computing the remaining
    # Fibonacci numbers based
    # on the previous two numbers
    while(l <= MAX):
        temp = curr + prev
        fib.add(temp)
        prev = curr
        curr = temp
        l += 1
 
# Function to find the
# pair of Fibonacci numbers
# with the given sum and
# minimum absolute difference
def findFibonacci(N):
   
    global fib
     
    # Start from N/2 such
    # that the difference
    # between i and N - i
    # will be minimum
    i = N//2
    while(i > 1):
       
        # If both 'i' and 'sum - i'
        # are fibonacci numbers then
        # print them and break the loop
        if (i in fib and
            (N - i) in fib):
            print(i, (N - i))
            return
        i -= 1
 
    # If there is no Fibonacci
    # pair possible
    print("-1")
 
# Driver code
if __name__ == '__main__':
   
    # Generate the Fibonacci
    # numbers
    fibonacci()
    sum = 199
     
    # Find the Fibonacci pair
    findFibonacci(sum)
     
# This code is contributed by bgangwar59


C#
// C# program to find the pair of
// Fibonacci numbers with a given sum
// and minimum absolute difference
using System;
using System.Collections.Generic;
 
class GFG{
    // Hash to store all the
    // Fibonacci numbers
    static int MAX=100005;
    static  HashSet fib=new HashSet();
      
    // Function to generate fibonacci Series
    // and create hash table
    // to check Fibonacci numbers
    static void fibonacci()
    {
        // Adding the first two Fibonacci
        // numbers into the Hash set
        int prev = 0, curr = 1, len = 2;
        fib.Add(prev);
        fib.Add(curr);
      
        // Computing the remaining Fibonacci
        // numbers based on the previous
        // two numbers
        while (len <= MAX) {
            int temp = curr + prev;
            fib.Add(temp);
            prev = curr;
            curr = temp;
            len++;
        }
    }
      
    // Function to find the pair of
    // Fibonacci numbers with the given
    // sum and minimum absolute difference
    static void findFibonacci(int N)
    {
      
        // Start from N/2 such that the
        // difference between i and
        // N - i will be minimum
        for (int i = N / 2; i > 1; i--) {
      
            // If both 'i' and 'sum - i' are
            // fibonacci numbers then print
            // them and break the loop
            if (fib.Contains(i)  && fib.Contains(N - i)) {
      
                Console.WriteLine(i+" "+(N - i));
                return;
            }
        }
      
        // If there is no Fibonacci pair
        // possible
        Console.WriteLine("-1");
    }
      
    // Driver code
    public static void Main(String []args)
    {
        // Generate the Fibonacci numbers
        fibonacci();
      
        int sum = 199;
      
        // Find the Fibonacci pair
        findFibonacci(sum);
    }
}
 
// This code is contributed by sapnasingh4991


输出:
55 144