java_day4

目标:Java web开发

题目要求及样例:耍杂技的牛

//新学的,都写在注释了
import java.io.*;
import java.util.*;
class Node implements Comparable<Node>{//重写接口
    int w,s;
    public Node(int w,int s){
        this.w=w;
        this.s=s;
    }
    public int compareTo(Node p){//重写方法,返回值为-1,0,1中的一种
        return Integer.compare(s+w,p.s+p.w);
        //Integer类中的compare方法传入两个参数a和b,返回值为-1(a<b时),0(a==b时),1(a>b时)   
    }
}
public class Main{
    static final int N=50010;
    static Node[] a=new Node[N];
    public static void main(String[] args) throws IOException{
        //throws IOException:因为有readLine(),必须要有报告异常
        /*对java main函数前public static void 的解释:
            main函数在类中,需要加static修饰才能在类没被定义时存在
            java虚拟机JVM调用main函数,main函数需要用public修饰才能被JVM发现
            JVM被设计为不接受任何返回值,所以返回值用void
        */
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        // InputStreamReader用BufferedReader包装,需有io包
        // BufferedReader类会尽量提取比当前操作所需的更多字节,提升提取效率
        int n=Integer.parseInt(in.readLine());//读取一行(这一行只有一个)字符串,转化为整数

        for(int i=0;i<n;i++){
            String[] S=in.readLine().split(" ");//读取一行字符串,根据空格分开
            int w=Integer.parseInt(S[0]);
            int s=Integer.parseInt(S[1]);
            a[i]=new Node(w,s);
        }
        Arrays.sort(a,0,n);//util包下的排序函数
        int res=(int)-2e9;//double类型的两种写法之一,要强制转换成int型
        int sum=0;
        for(int i=0;i<n;i++){
            res=Math.max(res,sum-a[i].s);
            sum+=a[i].w;
        }
        System.out.print(res);
    }
}

posted @ 2021-07-06 21:57  zhuangzhongxu  阅读(36)  评论(0)    收藏  举报