业余的业余连专业选手的尾气都闻不到,难受
题目描述
Bobo has two fractions a/x and b/y. 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 <= 10^18
1 <= a,b <= 10^9
There are at most 10^5 test cases.
输出描述:
For each test case
if x/a = y/b,Print ‘=’
if x/a < y/b,Print ‘<’
if x/a > y/b, Print ‘>’
示例1
输入
1 2 1 1
1 1 1 2
1 1 1 1
输出
<
>
=
题目目的很明确,比较2个分数的大小,但是有大数,注意边界
所以模拟一下分数,然后比较。 思路就是 x / a 与 y / b 的大小其实是 x * b 与 y * a 的大小,但是大数会溢出,所以可以取余。假设:
x / a = m + x1/a;
y / b = n + y1/b;
如果n>m, y/b > x/a,反之同理,如果相等,就比较余数即可。
#include<iostream>
using namespace std;
using ll = long long;
int main()
{
ll x,a,y,b;
while(cin>>x>>a>>y>>b){
ll x1 = x/a;
ll y1 = y/b;
int flag = 0;
if(x1 > y1) flag = 1;
else if(x1 < y1) flag = -1;
else if(x1 == y1)
{
ll x2 = x%a;
ll y2 = y%b;
x2 *= b;
y2 *= a;
if(x2 == y2) flag = 0;
else if(x2 > y2) flag = 1;
else flag = -1;
}
if(flag == 0)
{
cout<<'='<<endl;
}
else if(flag == 1)
{
cout<<'>'<<endl;
}
else
{
cout<<'<'<<endl;
}
}
return 0;
}