有一批分辨率为 a * b 的显示器,要将长宽比例调整为 x : y,问应该如何缩小显示器,使缩小后的面积最大。
容易出错的地方是x 和 y 可能不互质,所以要求出最大公约数,给 x :y约分。
#include<iostream>
using namespace std;
int gcd(int a, int b){
if(a == 0 || b == 0)
return a + b;
else
return gcd(b % a, a);
}
int main(){
__int64 a, b, x, y;
cin >> a >> b >> x >> y;
int temp = gcd(x, y);
x /= temp;
y /= temp;
if(x > a || y > b)
cout << "0 0" << endl;
else if(x * b > a * y){
cout << a / x * x << " " << a / x * y << endl;
}else if(x * b < a * y)
cout << b / y * x << " " << b / y * y << endl;
else
cout << a << " " << b << endl;
}