📜  检查两个数字是否成黄金比例

📅  最后修改于: 2021-04-27 09:35:03             🧑  作者: Mango

给定两个数字AB ,任务是检查A和B是否处于黄金比例。
黄金比例:如果两个数字的比例与两个数字之和与较大数字的比例相同,则称两个数字为黄金比例。这里a> b> 0,下面是黄金比例的几何表示:

\frac{A+B}{A} = \frac{A}{B} = \varphi = \frac{1+ \sqrt{5}}{2} = 1.618

例子:

Input: A = 1, B = 0.618
Output: Yes
Explanation:
These two numbers together forms Golden ratio

\frac{A}{B} = \frac{A + B}{A} = \frac{1.618}{1} = 1.618

Input: A = 61.77, B = 38.22

Output Yes

Explanation:

These two numbers together forms Golden ratio

\frac{A}{B} = \frac{A + B}{A} = \frac{99.99}{61.77} = 1.618

方法:想法是找到两个比率,并检查该比率是否等于黄金比率。那是1.618。

// Here A denotes the larger number

\frac{A}{B} = \frac{A + B}{A} = 1.618

下面是上述方法的实现:

C++
// C++ implementation to check 
// whether two numbers are in 
// golden ratio with each other
#include 
using namespace std;
 
// Function to check that two 
// numbers are in golden ratio
bool checkGoldenRatio(float a,
                      float b)
{
  // Swapping the numbers such 
  // that A contains the maximum
  // number between these numbers
  if(a <= b)
  {
    float temp = a;
    a = b;
    b = temp;
  }
 
  // First Ratio
  std::stringstream ratio1;
  ratio1 << std :: fixed <<
            std :: setprecision(3) <<
            (a / b);
 
  // Second Ratio
  std::stringstream ratio2;
  ratio2 << std :: fixed <<
            std :: setprecision(3) <<
            (a + b) / a;
 
  // Condition to check that two
  // numbers are in golden ratio
  if((ratio1.str() == ratio2.str()) &&
      ratio1.str() == "1.618")
  {
    cout << "Yes" << endl;
    return true;
  }
  else
  {
    cout << "No" << endl;
    return false;
  }
}
  
// Driver code
int main()
{
  float a = 0.618;
  float b = 1;
 
  // Function Call
  checkGoldenRatio(a, b);
 
  return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java implementation to check 
// whether two numbers are in 
// golden ratio with each other
class GFG{
     
// Function to check that two 
// numbers are in golden ratio
public static Boolean checkGoldenRatio(float a,
                                       float b)
{
     
    // Swapping the numbers such 
    // that A contains the maximum
    // number between these numbers
    if (a <= b)
    {
        float temp = a;
        a = b;
        b = temp;
    }
     
    // First Ratio
    String ratio1 = String.format("%.3f", a / b);
     
    // Second Ratio
    String ratio2 = String.format("%.3f", (a + b) / a);
     
    // Condition to check that two
    // numbers are in golden ratio
    if (ratio1.equals(ratio2) &&
        ratio1.equals("1.618"))
    {
        System.out.println("Yes");
        return true;
    }
    else
    {
        System.out.println("No");  
        return false;
    }
}
 
// Driver code
public static void main(String []args)
{
    float a = (float)0.618;
    float b = 1;
     
    // Function Call
    checkGoldenRatio(a, b);
}
}
 
// This code is contributed by rag2127


Python3
# Python3 implementation to check
# whether two numbers are in
# golden ratio with each other
 
# Function to check that two
# numbers are in golden ratio
def checkGoldenRatio(a, b):
     
    # Swapping the numbers such
    # that A contains the maximum
    # number between these numbers
    a, b = max(a, b), min(a, b)
     
    # First Ratio
    ratio1 = round(a/b, 3)
     
    # Second Ratio
    ratio2 = round((a+b)/a, 3)
    # Condition to check that two
    # numbers are in golden ratio
    if ratio1 == ratio2 and\
       ratio1 == 1.618:
        print("Yes")
        return True
    else:
        print("No")
        return False
         
# Driver Code
if __name__ == "__main__":
    a = 0.618
    b = 1
     
    # Function Call
    checkGoldenRatio(a, b)


C#
// C# implementation to check 
// whether two numbers are in 
// golden ratio with each other
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to check that two 
    // numbers are in golden ratio
    static bool checkGoldenRatio(float a,
                          float b)
    {
      // Swapping the numbers such 
      // that A contains the maximum
      // number between these numbers
      if(a <= b)
      {
        float temp = a;
        a = b;
        b = temp;
      }
      
      // First Ratio
      string ratio1 = String.Format("{0:0.000}", a / b);
      
      // Second Ratio
      string ratio2 = String.Format("{0:0.000}", (a + b) / a);
 
      // Condition to check that two
      // numbers are in golden ratio
      if(ratio1 == ratio2 && ratio1 == "1.618")
      {
        Console.WriteLine("Yes");
        return true;
      }
      else
      {
        Console.WriteLine("No");
        return false;
      }
    }
   
  // Driver code 
  static void Main() {
      float a = (float)0.618;
      float b = 1;
      
      // Function Call
      checkGoldenRatio(a, b);
  }
}
 
// This code is contributed by divyesh072019


输出:
Yes

参考: https : //en.wikipedia.org/wiki/Golden_ratio