【牛客网】2019多校训练(1)J Fraction Comparision

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

题意:分数比较大小

题解:签到题,使用C直接乘会炸 long long,可以使用Java,Python的大数,或者使用C的时候先比较分数的整数部分,再比较小数部分。

C++代码:

#include
using namespace std;
typedef long long int ll;

int main()
{
	ll x, a, y, b;
	while (cin >> x >> a >> y >> b)
	{
		if (x / a < y / b)
		{
			cout << "<" << endl;
		}
		else if (x / a == y / b)
		{
			ll temp1 = x % a;		//小数部分大小 
			ll temp2 = y % b;
			temp1 = temp1 * b;		//交换分子后相乘 
			temp2 = temp2 * a;
			if (temp1 < temp2)
				cout << "<" << endl;
			else if (temp1 == temp2)
				cout << "=" << endl;
			else
				cout << ">" << endl;
		}
		else
		{
			cout << ">" << endl;
		}
	}
	return 0;
}

JAVA代码:

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

Python:

try:
    while True:
        s=input()
        s=s.split(' ')
        if(int(s[0])*int(s[3])")
except EOFError:
    pass

 

你可能感兴趣的:(牛客网)