2019牛客暑期多校1J题

登录—专业IT笔试面试备考平台_牛客网

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.
* 0x,y1018 * 1a,b109 * There are at most 105 test cases.

输出描述:

For each test case, print `=` if x/a=y/b. Print `<` if x/a<y/b. Print `>` otherwise.

这题如果x,y,a,b均为质数,那么使用x*b和y*a来判断超过了long long的位数,如果直接除会造成精度缺失;
所以我们可以考虑把整数部分和小数部分分开求,用a1来代表x/a(整数部分),a2来代表x%a(小数部分),y/b同理;
对于整数部分相同的,我们可以让小数部分进行通分比大小(防止精度损失)
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll x,y,a,b;
int main(){
    while(~scanf("%lld%lld%lld%lld",&x,&a,&y,&b)){
        ll a1=x/a,b1=y/b;
        ll a2=x%a*b,b2=y%b*a;
        if(a1>b1||(a1==b1&&a2>b2))
            puts(">");
        else if(a1<b1||(a1==b1&&a2<b2))
            puts("<");
        else
            puts("=");
    }
}

全部评论

相关推荐

09-17 19:25
已编辑
太原理工大学 游戏测试
叁六玖:公司名发我,我要这个HR带我打瓦
我的秋招日记
点赞 评论 收藏
分享
09-19 13:59
门头沟学院 Java
用微笑面对困难:Trae一下,如果真成了,他用了直接发字节起诉代码版权,,这个代码不商用是没问题的如果没成也是情理之中的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务