2019牛客多校第一场J题——Fraction Comparision

题目传送门

大致题意:

依次输入x,a,y,b,比较x/a和y/b的大小,前者大则输出“>”,后者大则输出“<”,相等则输出“=”。
0<=x,y<=10^18
1<=a,b<=10^9

解法一(正解):

不能x/a和y/b直接去除比较,会爆double;

也不能直接交叉相乘后去比较,会爆long long;

故而可以化成如下的带分数形式:
在这里插入图片描述

解法二:C/C++高精度大数乘法

#include
#include
#include
using namespace std;
int main()
{
    string str1,str2,str3,str4;
    int a[250],b[250],c[500],d[500],len1,len2;  //250位以内的两个数相乘
    int i,j;
    while(!(cin>>str1>>str3>>str4>>str2).eof())
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        a[0]=str1.length();
        for(i=1; i<=a[0]; i++)
            a[i]=str1[a[0]-i]-'0';
        b[0]=str2.length();
        for(i=1; i<=b[0]; i++)
            b[i]=str2[b[0]-i]-'0';
        memset(c,0,sizeof(c));
        for(i=1; i<=a[0]; i++) //做按位乘法同时处理进位,注意循环内语句的写法。
            for(j=1; j<=b[0]; j++)
            {
                c[i+j-1]+=a[i]*b[j];
                c[i+j]+=c[i+j-1]/10;
                c[i+j-1]%=10;
            }
        len1=a[0]+b[0]+1;  //去掉最高位的0,然后输出
        while((c[len1]==0)&&(len1>1))
            len1--;   //为什么此处要len>1??
        /*for(i=len1;i>=1;i--)
        	cout<
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        a[0]=str3.length();
        for(i=1; i<=a[0]; i++)
            a[i]=str3[a[0]-i]-'0';
        b[0]=str4.length();
        for(i=1; i<=b[0]; i++)
            b[i]=str4[b[0]-i]-'0';
        memset(d,0,sizeof(d));
        for(i=1; i<=a[0]; i++) //做按位乘法同时处理进位,注意循环内语句的写法。
            for(j=1; j<=b[0]; j++)
            {
                d[i+j-1]+=a[i]*b[j];
                d[i+j]+=d[i+j-1]/10;
                d[i+j-1]%=10;
            }
        len2=a[0]+b[0]+1;  //去掉最高位的0,然后输出
        while((d[len2]==0)&&(len2>1))
            len2--;   //为什么此处要len>1??
        /*for(i=len2;i>=1;i--)
         	cout<
        if(len1>len2)
            printf(">\n");
        else if(len1<len2)
            printf("<\n");
        else
        {
            for(int i=len1; i>=1; i--)
            {
                if(c[i]>d[i])
                {
                    printf(">\n");
                    break;
                }
                else if(c[i]<d[i])
                {
                    printf("<\n");
                    break;
                }
                if(c[i]==d[i]&&i==1){
                    printf("=\n");
                	break;
                }
            }
        }
    }
    return 0;

解法三:Java大数

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
     
    void MAIN() {
         
        Scanner cin=new Scanner(System.in);
 
        while(cin.hasNext()) {
            BigInteger x=cin.nextBigInteger();
            BigInteger a=cin.nextBigInteger();
            BigInteger y=cin.nextBigInteger();
            BigInteger b=cin.nextBigInteger();
             
            BigInteger L=x.multiply(b);
            BigInteger R=y.multiply(a);
             
            if(L.compareTo(R)<0) System.out.println("<");
            else if(L.compareTo(R)>0) System.out.println(">");
            else System.out.println("=");
        }
    }
     
    public static void main(String[] args) {
        new Main().MAIN();
    }
 
}
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            BigInteger a, b, c, d;
            a = cin.nextBigInteger();
            b = cin.nextBigInteger();
            c = cin.nextBigInteger();
            d = cin.nextBigInteger();
            a = a.multiply(d);
            b = b.multiply(c);
            if(a.compareTo(b) > 0) {
                System.out.println(">");
            }
            else if(a.compareTo(b) == 0) {
                System.out.println("=");
            }
            else{
                System.out.println("<");
            }
        }
        cin.close();
    }
}
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
        public static void main(String []args){
                Scanner sc = new Scanner(System.in);
                while(sc.hasNext()){
                        String x=sc.next();
                        String a=sc.next();
                        String y=sc.next();
                        String b=sc.next();
                        BigInteger xx=new BigInteger(x);
                        BigInteger aa=new BigInteger(a);
                        BigInteger yy=new BigInteger(y);
                        BigInteger bb=new BigInteger(b);
                        BigInteger ans1=bb.multiply(xx);
                        BigInteger ans2=aa.multiply(yy);
                        if(ans1.compareTo(ans2)==-1)
                                System.out.println('<');
                        else if(ans1.compareTo(ans2)==0)
                                System.out.println('=');
                        else
                                System.out.println('>');
                }
        }
}

解法三:python3

try:
    while True:
        s=input()
        s=s.split(' ')
        if(int(s[0])*int(s[3])<int(s[1])*int(s[2])):
            print("<")
        elif(int(s[0])*int(s[3])==int(s[1])*int(s[2])):
            print("=")
        else:
            print(">")
except EOFError:
    pass
while 1:
    try:
        x,a,y,b=input().split()
        x = int(x)
        y = int(y)
        a = int(a)
        b = int(b)
        if x*b==y*a:
            print("=")
        elif x*b<y*a:
            print("<")
        else:
            print(">")
    except:
        break
while True:
    try:
        s = input()
        x, a, y, b = map(int, s.split(" "))
        ans1 = x * b
        ans2 = y * a
        if ans1 < ans2:
            print("<")
        elif ans1 == ans2:
            print("=")
        else:
            print(">")
    except EOFError:
        break

解法四:__int128

#include 
using namespace std; 
__int128 x,a,y,b;
int main()
{
    long long q,w,e,r;
    while(cin>>q>>w>>e>>r){
        x=q,a=w,y=e,b=r;
        if(x*b==a*y)
            puts("=");
        else if(x*b<a*y)
            puts("<");
        else
            puts(">");
    }
    return 0;
}

你可能感兴趣的:(题解)