给定两个矩形,X的长宽比为a:b ,Y的长宽比为c:d 。只要边的比例保持不变,就可以调整两个矩形的大小。任务是将第二个矩形放置在第一个矩形内,以使至少一个边相等,并且两个矩形的边重叠,并求出(第二个矩形所占据的空间):(第一个矩形所占的空间)的比率。
例子:
Input: a = 1, b = 1, c = 3, d = 2
Output: 2:3
The dimensions can be 3X3 and 3X2.
Input: a = 4, b = 3, c = 2, d = 2
Output: 3:4
The dimensions can be 4X3 and 3X3
方法:如果我们使矩形的一侧相等,则所需的比率将是另一侧的比率。
考虑2种情况:
由于将比率的两边相乘不会改变其值。首先尝试使a和c相等,可以通过将(a:b)乘以lcm / a和(c:d)乘以lcm / c使其等于lcm。乘法之后,(b:d)的比率将是必需的答案。可以通过将b和d除以gcd(b,d)来降低该比率。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to find the ratio
void printRatio(int a, int b, int c, int d)
{
if (b * c > a * d) {
swap(c, d);
swap(a, b);
}
// LCM of numerators
int lcm = (a * c) / __gcd(a, c);
int x = lcm / a;
b *= x;
int y = lcm / c;
d *= y;
// Answer in reduced form
int k = __gcd(b, d);
b /= k;
d /= k;
cout << b << ":" << d;
}
// Driver code
int main()
{
int a = 4, b = 3, c = 2, d = 2;
printRatio(a, b, c, d);
return 0;
}
Java
// Java implementation of above approach
import java.io.*;
class GFG {
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function to find the ratio
static void printRatio(int a, int b, int c, int d)
{
if (b * c > a * d) {
int temp = c;
c =d;
d =c;
temp =a;
a =b;
b=temp;
}
// LCM of numerators
int lcm = (a * c) / __gcd(a, c);
int x = lcm / a;
b *= x;
int y = lcm / c;
d *= y;
// Answer in reduced form
int k = __gcd(b, d);
b /= k;
d /= k;
System.out.print( b + ":" + d);
}
// Driver code
public static void main (String[] args) {
int a = 4, b = 3, c = 2, d = 2;
printRatio(a, b, c, d);
}
}
// This code is contributed by inder_verma..
Python3
import math
# Python3 implementation of above approach
# Function to find the ratio
def printRatio(a, b, c, d):
if (b * c > a * d):
swap(c, d)
swap(a, b)
# LCM of numerators
lcm = (a * c) / math.gcd(a, c)
x = lcm / a
b = int(b * x)
y = lcm / c
d = int(d * y)
# Answer in reduced form
k = math.gcd(b,d)
b =int(b / k)
d = int(d / k)
print(b,":",d)
# Driver code
if __name__ == '__main__':
a = 4
b = 3
c = 2
d = 2
printRatio(a, b, c, d)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of above approach
using System;
class GFG {
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function to find the ratio
static void printRatio(int a, int b, int c, int d)
{
if (b * c > a * d) {
int temp = c;
c =d;
d =c;
temp =a;
a =b;
b=temp;
}
// LCM of numerators
int lcm = (a * c) / __gcd(a, c);
int x = lcm / a;
b *= x;
int y = lcm / c;
d *= y;
// Answer in reduced form
int k = __gcd(b, d);
b /= k;
d /= k;
Console.WriteLine( b + ":" + d);
}
// Driver code
public static void Main () {
int a = 4, b = 3, c = 2, d = 2;
printRatio(a, b, c, d);
}
}
// This code is contributed by inder_verma..
PHP
$b)
return __gcd($a - $b, $b);
return __gcd($a, $b - $a);
}
// Function to find the ratio
function printRatio($a, $b, $c, $d)
{
if ($b * $c > $a * $d)
{
$temp = $c;
$c = $d;
$d = $c;
$temp = $a;
$a = $b;
$b = $temp;
}
// LCM of numerators
$lcm = ($a * $c) / __gcd($a, $c);
$x = $lcm / $a;
$b *= $x;
$y = $lcm / $c;
$d *= $y;
// Answer in reduced form
$k = __gcd($b, $d);
$b /= $k;
$d /= $k;
echo $b . ":" . $d;
}
// Driver code
$a = 4; $b = 3; $c = 2; $d = 2;
printRatio($a, $b, $c, $d);
// This code is contributed
// by Akanksha Rai
?>
Javascript
输出:
3:4