2019牛客暑期多校训练营(第一场场)_J题Fraction Comparision

题目链接: https://ac.nowcoder.com/acm/contest/881/J

题意:输入x, a, y, b 问 x/a 与 y/b 的大小关系!

思路:直接相除肯定是不行的会有精度丢失,以及 数字太大爆long long 的情况!所以可以考虑用Java 大数去判断 xb 与 ya 的大小关系!或者 用带分数进行比较!

Java代码

import java.math.BigInteger;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        while(sc.hasNext()){
            BigInteger a, b, x , y;
            x = sc.nextBigInteger();
            a = sc.nextBigInteger();
            y = sc.nextBigInteger();
            b = sc.nextBigInteger();
            if(b.multiply(x).compareTo(a.multiply(y))==1){
                System.out.println(">");
            }
            else if(b.multiply(x).compareTo(a.multiply(y))==0){
                System.out.println("=");
            }
            else System.out.println("<");
        }
    }
}

C++代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    long long x, a, y, b;
    while(cin >> x >> a >> y >> b){
        long long na, nb;
        na = (x / a);
        x %= a;
        nb = (y / b);
        y %= b;
        if(na > nb){
            cout << ">" << endl;
        }
        else if(na < nb){
            cout << "<" << endl;
        }
        else{
            if(x * b < y * a ){
                cout << "<" << endl;
            }
            else if(x * b > y * a){
                cout << ">" << endl;
            }
            else{
                cout << "=" << endl;
            }
        }
    }
    return 0;
}

发布了27 篇原创文章 · 获赞 5 · 访问量 2502

猜你喜欢

转载自blog.csdn.net/HKer_YM/article/details/97824774