2019牛客暑期多校训练营(第一场)

链接

J Fraction Comparision

签到题,比较两个分数的大小,但是交叉相乘会导致溢出。

那当然是依靠Java去解决啦。只是这个是真的太慢了,跑了差不多1700ms,还费了一大堆内存。

package acscut;

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

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 p1=x.multiply(b);
            BigInteger p2=y.multiply(a);
            if(p1.compareTo(p2)==0) {
                System.out.println("=");
            }
            else if(p1.compareTo(p2)>0) {
                System.out.println(">");
            }
            else {
                System.out.println("<");
            }
        }
        sc.close();
    }
}

使用Long进行读入会稍微快一点。

package acscut;

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

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            long tmp;
            tmp=sc.nextLong();
            BigInteger x=BigInteger.valueOf(tmp);
            tmp=sc.nextLong();
            BigInteger a=BigInteger.valueOf(tmp);
            tmp=sc.nextLong();
            BigInteger y=BigInteger.valueOf(tmp);
            tmp=sc.nextLong();
            BigInteger b=BigInteger.valueOf(tmp);
            x=x.multiply(b);
            y=y.multiply(a);
            int res=x.compareTo(y);
            if(res==0) 
                System.out.println("=");
            else if(res>0) 
                System.out.println(">");
            else 
                System.out.println("<");
        }
        sc.close();
    }
}

从别人手里偷了一个快读,貌似BigInteger不能直接next,其他的功能都齐全了。

import java.io.*;
import java.math.*;
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            long tmp;
            tmp=sc.nextLong();
            BigInteger x=BigInteger.valueOf(tmp);
            tmp=sc.nextLong();
            BigInteger a=BigInteger.valueOf(tmp);
            tmp=sc.nextLong();
            BigInteger y=BigInteger.valueOf(tmp);
            tmp=sc.nextLong();
            BigInteger b=BigInteger.valueOf(tmp);
            x=x.multiply(b);
            y=y.multiply(a);
            int res=x.compareTo(y);
            if(res==0)
                System.out.println("=");
            else if(res>0)
                System.out.println(">");
            else
                System.out.println("<");
        }
    }
}
 
class Scanner {
    private BufferedReader br;
    private StringTokenizer st;
  
    Scanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(in));
        st = new StringTokenizer("");
    }
  
    String nextLine() {
        try {
            return br.readLine();
        } catch (IOException e) {
            throw new IOError(e);
        }
    }
  
    boolean hasNext() {
        while (!st.hasMoreTokens()) {
            String s = nextLine();
            if (s == null) {
                return false;
            }
            st = new StringTokenizer(s);
        }
        return true;
    }
  
    String next() {
        hasNext();
        return st.nextToken();
    }
  
    int nextInt() {
        return Integer.parseInt(next());
    }
  
    long nextLong() {
        return Long.parseLong(next());
    }
  
    double nextDouble() {
        return Double.parseDouble(next());
    }
}

转载于:https://www.cnblogs.com/Inko/p/11383130.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值