2019牛客暑期多校训练营(第一场)J题(签到题)
登录—专业IT笔试面试备考平台_牛客网
https://ac.nowcoder.com/acm/contest/881/J
本题就是比较两个分数的大小,毫无压力的签到水题,x : a 与 y : b,比较内项积与外项积即可。
因为数比较大,可能会爆栈,所以个人感觉python和java大数比较好用,亲测该题python代码要优于java大数。
题目描述
链接:https://ac.nowcoder.com/acm/contest/881/J
来源:牛客网
来源:牛客网
Bobo has two fractions xaxa and ybyb. He wants to compare them. Find the result.
输入描述:
The input consists of several test cases and is terminated by end-of-file. Each test case contains four integers x, a, y, b. * 0≤x,y≤10180≤x,y≤1018 * 1≤a,b≤1091≤a,b≤109 * There are at most 105105 test cases.
输出描述:
For each test case, print `=` if xa=ybxa=yb. Print `<` if xa<ybxa<yb. Print `>` otherwise.
python代码
while True:
try:
s = input()
x, a, y, b = map(int, s.split(" "))
ans1 = x * b
ans2 = y * a
if ans1 < ans2:
print("<")
elif ans1 == ans2:
print("=")
else:
print(">")
except EOFError:
break |
java代码
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext()) {
BigInteger a, b, c, d;
a = cin.nextBigInteger();
b = cin.nextBigInteger();
c = cin.nextBigInteger();
d = cin.nextBigInteger();
a = a.multiply(d);
b = b.multiply(c);
if(a.compareTo(b) > 0) {
System.out.println(">");
}
else if(a.compareTo(b) == 0) {
System.out.println("=");
}
else{
System.out.println("<");
}
}
cin.close();
}
} |
最后附上本题的标程(大神的标准答案):
#include <bits/stdc++.h>
static std::pair<uint64_t, uint64_t> fcompare(uint64_t x, uint32_t a,
uint64_t y, uint32_t b) {
uint64_t p = x / a; // p <= (x / a) < p + 1
uint64_t q = y / b; // q <= (y / b) < q + 1
if (p != q) {
return {p, q};
}
x %= a;
y %= b;
return {x * b, y * a};
}
int main() {
std::ios::sync_with_stdio(false);
uint64_t x, y;
uint32_t a, b;
while (std::cin >> x >> a >> y >> b) {
auto result = fcompare(x, a, y, b);
if (result.first == result.second) {
puts("=");
} else if (result.first < result.second) {
puts("<");
} else {
puts(">");
}
}
}
