给定正整数N ,任务是找到最接近给定整数N的斐波那契数。如果有两个斐波那契数与N相同,则打印较小的值。
例子:
Input: N = 20
Output: 21
Explanation: Nearest Fibonacci number to 20 is 21.
Input: N = 17
Output: 13
方法:请按照以下步骤解决问题:
- 如果N等于0 ,则输出0作为结果。
- 初始化一个变量,例如ans ,以存储最接近N的斐波那契数。
- 初始化两个变量,例如,将First设为0 ,将Second设为1 ,以存储斐波那契数列的第一项和第二项。
- 将First和Second的和存储在变量中,例如Third 。
- 迭代直到Third的值最大为N并执行以下步骤:
- 将“第一至第二”和“第二至第三”的值更新。
- 将First和Second的和存储在变量Third中。
- 如果Second和N的绝对差最大为Third和N的值,则将ans的值更新为Second 。
- 否则,将ans的值更新为Third 。
- 完成上述步骤后,输出ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Fibonacci
// number which is nearest to N
void nearestFibonacci(int num)
{
// Base Case
if (num == 0) {
cout << 0;
return;
}
// Initialize the first & second
// terms of the Fibonacci series
int first = 0, second = 1;
// Store the third term
int third = first + second;
// Iterate until the third term
// is less than or equal to num
while (third <= num) {
// Update the first
first = second;
// Update the second
second = third;
// Update the third
third = first + second;
}
// Store the Fibonacci number
// having smaller difference with N
int ans = (abs(third - num)
>= abs(second - num))
? second
: third;
// Print the result
cout << ans;
}
// Driver Code
int main()
{
int N = 17;
nearestFibonacci(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the Fibonacci
// number which is nearest to N
static void nearestFibonacci(int num)
{
// Base Case
if (num == 0)
{
System.out.print(0);
return;
}
// Initialize the first & second
// terms of the Fibonacci series
int first = 0, second = 1;
// Store the third term
int third = first + second;
// Iterate until the third term
// is less than or equal to num
while (third <= num)
{
// Update the first
first = second;
// Update the second
second = third;
// Update the third
third = first + second;
}
// Store the Fibonacci number
// having smaller difference with N
int ans = (Math.abs(third - num) >=
Math.abs(second - num)) ?
second : third;
// Print the result
System.out.print(ans);
}
// Driver Code
public static void main (String[] args)
{
int N = 17;
nearestFibonacci(N);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to find the Fibonacci
# number which is nearest to N
def nearestFibonacci(num):
# Base Case
if (num == 0):
print(0)
return
# Initialize the first & second
# terms of the Fibonacci series
first = 0
second = 1
# Store the third term
third = first + second
# Iterate until the third term
# is less than or equal to num
while (third <= num):
# Update the first
first = second
# Update the second
second = third
# Update the third
third = first + second
# Store the Fibonacci number
# having smaller difference with N
if (abs(third - num) >=
abs(second - num)):
ans = second
else:
ans = third
# Print the result
print(ans)
# Driver Code
if __name__ == '__main__':
N = 17
nearestFibonacci(N)
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the Fibonacci
// number which is nearest to N
static void nearestFibonacci(int num)
{
// Base Case
if (num == 0)
{
Console.Write(0);
return;
}
// Initialize the first & second
// terms of the Fibonacci series
int first = 0, second = 1;
// Store the third term
int third = first + second;
// Iterate until the third term
// is less than or equal to num
while (third <= num)
{
// Update the first
first = second;
// Update the second
second = third;
// Update the third
third = first + second;
}
// Store the Fibonacci number
// having smaller difference with N
int ans = (Math.Abs(third - num) >=
Math.Abs(second - num)) ?
second : third;
// Print the result
Console.Write(ans);
}
// Driver Code
public static void Main(string[] args)
{
int N = 17;
nearestFibonacci(N);
}
}
// This code is contributed by sanjoy_62
Javascript
输出:
13
时间复杂度: O(log N)
辅助空间: O(1)