输入一行包含四个整数,
,
和
.
输出两个整数表示满足条件的和
.若不存在,则输出"0 0".
1 1 2 1
0 0
1000 500 4 2
1000 500
1000 500 3 1
999 333
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().trim().split(" "); int A = Integer.parseInt(params[0]), B = Integer.parseInt(params[1]), a = Integer.parseInt(params[2]), b = Integer.parseInt(params[3]); int greatestCommonDivisor = gcd(a, b); // 先约分 a /= greatestCommonDivisor; b /= greatestCommonDivisor; // 然后计算unit int unit = Math.min(A / a, B / b); // x占a份unit,y占b份unit System.out.println(unit * a + " " + unit * b); } private static int gcd(int a, int b){ if(a < b){ int temp = a; a = b; b = temp; } while(b > 0){ int temp = a % b; a = b; b = temp; } return a; } }scala版本
import scala.io.StdIn object Main { def main(args: Array[String]): Unit = { val row = StdIn.readLine.trim.split(" ") val A = row(0).toInt val B = row(1).toInt var a = row(2).toInt var b = row(3).toInt val greatestCommonDivisor = gcd(a, b); a /= greatestCommonDivisor; b /= greatestCommonDivisor; val unit = math.min(A / a, B / b) println(s"${a * unit} ${b * unit}") } def gcd(num1: Int, num2: Int): Int = { var a = num1 var b = num2 if(a < b){ val temp = a a = b b = temp } while(b > 0){ val temp = a % b a = b b = temp } a } }
由于已经确定, 可以求出其最简分式为
其中
为a和b的最大公约数
由于存在一个倍数使得
与上式相等, 故只需要找到符合条件的最大的
即可, 可通过比较
和
的大小得到
#include <iostream> using namespace std; #define int long long int gcd(int a, int b) { return a%b?gcd(b, a%b):b; } signed main() { int a, b, c ,d; cin >> a >> b >> c >> d; int t = gcd(c, d); c /= t, d /= t; if( a < c && b < d) { cout << "0 0" << endl; return 0; } t = min(a / c, b / d); cout << c*t << " " << d*t << endl; }
#include <iostream> using namespace std; int gcd(int a, int b){ if(b > 0) return gcd(b, a % b); return a; } int main() { int A,B,a,b; cin>>A>>B>>a>>b; int num = gcd(a, b); a=a/num; b=b/num; int x,y; int isfind=0; for(y=B; y>=1; y--) { if(a*y%b==0&&a*y/b<=A) { x=y*a/b; isfind=1; break; } } if(isfind==0) cout<<0<<" "<<0; else cout<<x<<" "<<y; return 0; }
#include<bits/stdc++.h> using namespace std; int gcd(int a, int b){ if(b > 0) return gcd(b, a % b); return a; } int main(){ int A, B, a, b; while(cin >> A >> B >> a >> b){ int num = gcd(a, b); a /= num; b /= num; int i = a, j = b; int turns = min(A / a, B / b); if(turns == 0) cout << "0 0" << endl; else cout << a * turns << " " << b * turns << endl; } }
def niuke2(): A, B, a, b = [int(i) for i in input().split()] gcd_ab = gcd(a, b) x_min = a / gcd_ab y_min = b / gcd_ab mul = min(int(A / x_min), int(B / y_min)) return str(int(x_min * mul))+' '+str(int(y_min * mul)) # 求最大公约数 def gcd(mm: int, nn: int) -> int: m = max(mm, nn) n = min(mm, nn) rem = 1 while rem: rem = m % n m = n n = rem return m print(niuke2())
#include<iostream> #include<vector> #include<bits/stdc++.h> using namespace std; typedef unsigned long long ll; typedef unsigned int u8; ll A,B,a,b; int MaxFactor(ll a,ll b) { ll c=b; while(a%b!=0) { c=a%b; a=b; b=c; } return c; } int main() { cin>>A>>B>>a>>b; int MaxCom=MaxFactor(a,b); int simp_a=a/MaxCom,simp_b=b/MaxCom; int k; if((b*A)/(a*B)) k=B/simp_b; else k=A/simp_a; u8 x=k*simp_a; u8 y=k*simp_b; cout<<x<<" "<<y<<endl; return 0; }