来自给定数组 (a[i], b[j]) 的不同对使得 (a[i] + b[j]) 是斐波那契数
给定两个数组a[]和b[] ,任务是计算对(a[i], b[j])使得(a[i] + b[j])是一个斐波那契数。注意(a, b)等于(b, a)并且将被计算一次。
前几个斐波那契数是:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, …..
例子:
Input: a[] = {99, 1, 33, 2}, b[] = {1, 11, 2}
Output: 4
Total distinct pairs are (1, 1), (1, 2), (33, 1) and (2, 11)
Input: a[] = {5, 0, 8}, b[] = {0, 9}
Output: 3
方法:
- 取一个空集。
- 运行两个嵌套循环以从两个数组中生成所有可能的对,从第一个数组中获取一个元素(称为 a),从第二个数组中获取一个元素(称为 b)。
- 对(a + b)应用斐波那契测试,即为了使数字x成为斐波那契数, 5 * x 2 + 4或5 * x 2 – 4中的任何一个都必须是完美正方形。
- 如果它是斐波那契数,则将(a, b)推入集合中,如果a < b或(b, a)如果b < a 。这样做是为了避免重复。
- 最后集合的大小是有效对的总数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that returns true if
// x is a perfect square
bool isPerfectSquare(long double x)
{
// Find floating point value of
// square root of x
long double sr = sqrt(x);
// If square root is an integer
return ((sr - floor(sr)) == 0);
}
// Function that returns true if
// n is a Fibonacci Number
bool isFibonacci(int n)
{
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
// Function to return the count of distinct pairs
// from the given array such that the sum of the
// pair elements is a Fibonacci number
int totalPairs(int a[], int b[], int n, int m)
{
// Set is used to avoid duplicate pairs
set > s;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// If sum is a Fibonacci number
if (isFibonacci(a[i] + b[j]) == true) {
if (a[i] < b[j])
s.insert(make_pair(a[i], b[j]));
else
s.insert(make_pair(b[j], a[i]));
}
}
}
// Return the size of the set
return s.size();
}
// Driver code
int main()
{
int a[] = { 99, 1, 33, 2 };
int b[] = { 1, 11, 2 };
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[0]);
cout << totalPairs(a, b, n, m);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function that returns true if
// x is a perfect square
static boolean isPerfectSquare(double x)
{
// Find floating point value of
// square root of x
double sr = Math.sqrt(x);
// If square root is an integer
return ((sr - Math.floor(sr)) == 0);
}
// Function that returns true if
// n is a Fibonacci Number
static boolean isFibonacci(int n)
{
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
// Function to return the count of distinct pairs
// from the given array such that the sum of the
// pair elements is a Fibonacci number
static int totalPairs(int a[], int b[],
int n, int m)
{
// Set is used to avoid duplicate pairs
List s = new LinkedList<>();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// If sum is a Fibonacci number
if (isFibonacci(a[i] + b[j]) == true)
{
if (a[i] < b[j])
{
if(checkDuplicate(s, new pair(a[i], b[j])))
s.add(new pair(a[i], b[j]));
}
else
{
if(checkDuplicate(s, new pair(b[j], a[i])))
s.add(new pair(b[j], a[i]));
}
}
}
}
// Return the size of the set
return s.size();
}
static boolean checkDuplicate(List pairList,
pair newPair)
{
for(pair p: pairList)
{
if(p.first == newPair.first &&
p.second == newPair.second)
return false;
}
return true;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 99, 1, 33, 2 };
int b[] = { 1, 11, 2 };
int n = a.length;
int m = b.length;
System.out.println(totalPairs(a, b, n, m));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
from math import sqrt,floor
# Function that returns true if
# x is a perfect square
def isPerfectSquare(x) :
# Find floating point value of
# square root of x
sr = sqrt(x)
# If square root is an integer
return ((sr - floor(sr)) == 0)
# Function that returns true if
# n is a Fibonacci Number
def isFibonacci(n ) :
return (isPerfectSquare(5 * n * n + 4) or
isPerfectSquare(5 * n * n - 4))
# Function to return the count of distinct pairs
# from the given array such that the sum of the
# pair elements is a Fibonacci number
def totalPairs(a, b, n, m) :
# Set is used to avoid duplicate pairs
s = set();
for i in range(n) :
for j in range(m) :
# If sum is a Fibonacci number
if (isFibonacci(a[i] + b[j]) == True) :
if (a[i] < b[j]) :
s.add((a[i], b[j]));
else :
s.add((b[j], a[i]));
# Return the size of the set
return len(s);
# Driver code
if __name__ == "__main__" :
a = [ 99, 1, 33, 2 ];
b = [ 1, 11, 2 ];
n = len(a);
m = len(b);
print(totalPairs(a, b, n, m));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
public class pair
{
public int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function that returns true if
// x is a perfect square
static bool isPerfectSquare(double x)
{
// Find floating point value of
// square root of x
double sr = Math.Sqrt(x);
// If square root is an integer
return ((sr - Math.Floor(sr)) == 0);
}
// Function that returns true if
// n is a Fibonacci Number
static bool isFibonacci(int n)
{
return isPerfectSquare(5 * n * n + 4) ||
isPerfectSquare(5 * n * n - 4);
}
// Function to return the count of distinct pairs
// from the given array such that the sum of the
// pair elements is a Fibonacci number
static int totalPairs(int []a, int []b,
int n, int m)
{
// Set is used to avoid duplicate pairs
List s = new List();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// If sum is a Fibonacci number
if (isFibonacci(a[i] + b[j]) == true)
{
if (a[i] < b[j])
{
if(checkDuplicate(s, new pair(a[i], b[j])))
s.Add(new pair(a[i], b[j]));
}
else
{
if(checkDuplicate(s, new pair(b[j], a[i])))
s.Add(new pair(b[j], a[i]));
}
}
}
}
// Return the size of the set
return s.Count;
}
static bool checkDuplicate(List pairList,
pair newPair)
{
foreach(pair p in pairList)
{
if(p.first == newPair.first &&
p.second == newPair.second)
return false;
}
return true;
}
// Driver code
public static void Main(String[] args)
{
int []a = { 99, 1, 33, 2 };
int []b = { 1, 11, 2 };
int n = a.Length;
int m = b.Length;
Console.WriteLine(totalPairs(a, b, n, m));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
4