传送门: https://ac.nowcoder.com/acm/contest/881/J
题目描述
Bobo has two fractions x/a and y/b. 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.
x , y
a , b
There are at most
test cases
输出描述
For each test case, print ' = ' if x/a = y/b.
Print ' < ' if x/a < y/b.
Print ' > ' otherwise.
输入
1 2 1 1 1 1 1 2 1 1 1 1
输出
< > =
这道题原本就是道签到题,我上来想的就比较少,直接暴力搞,无奈数据太大,long long也存不下,long double也试过了。大概瞎搞了一个多小时,Wa了6发,看这榜上一个个都过了,有点难受。后来换了一个思路,突然灵机一动就想到了:先除取整 整数部分大就输出 > ,小就输出 <, 相等的话再取余 ,比较交叉相乘余数部分 。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long x,a,y,b;
long long n,m;
long long k,t;
while(scanf("%lld%lld%lld%lld",&x,&a,&y,&b)!=EOF)
{
n = x/a;
m = y/b;
if(n<m)
cout<<"<"<<endl;
else if(n>m)
cout<<">"<<endl;
else
{
k=x%a;
t=y%b;
if(k*b==t*a)
cout<<"="<<endl;
else if(k*b>t*a)
cout<<">"<<endl;
else
cout<<"<"<<endl;
}
}
return 0;
}赛后看了Rank的代码,发现有的是用 __int128写的,之前也没听过,感觉就是比long long再大一点的数据类型,上网上搜了一下,说是只能在Linux环境下使用 __int128,而且 __int128没有办法使用cin、cout来进行输入输出。所以输入输出需要自己写,但它确实也能储存1e35的数字。但这道题输入最多long long型,而输出只需要输出字符,同时 __int128支持比较,有些OJ平台貌似不支持 __int128,牛客应该支持,所以也可以这么写…
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int a,b;
LL x,y;
while(scanf("%lld%d%lld%d",&x,&a,&y,&b)==4){
__int128 l = (__int128)x * b;
__int128 r = (__int128)y * a;
if(l>r){
printf(">");
}
else if(l<r){
printf("<");
}
else{
printf("=");
}
printf("\n");
}
return 0;
}
有关__int128的具体使用方法可以参考
https://blog.csdn.net/shadandeajian/article/details/81843805

京公网安备 11010502036488号