zoukankan      html  css  js  c++  java
  • HDOJ 1009

    这道题比较简单,但是一直MLE.

    import java.util.Arrays;
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args)
        {
            Scanner scanner=new Scanner(System.in);
            short M=0,N=0,j,f;
            Food[] fs;
            while(true)
            {
                M=scanner.nextShort();
                N=scanner.nextShort();
                if(N==-1 && M==-1)
                    break;
                fs=new Food[N];
                for(short i=0;i<N;i++)
                {
                    fs[i]=new Food(scanner.nextShort(),scanner.nextShort());
                }
                Arrays.sort(fs);//注意这里的N和String的subString的endIndex一样都不包含在内
                float sum=0;
                short i=0;
                while(M>0 && i<N)
                {
                    if(fs[i].f>=M)
                    {
                        sum+=M*fs[i].j*1.0f/fs[i].f;
                        M=(short)(M-M);
                    }
                    else {
                        sum+=fs[i].j;
                        M=(short)(M-fs[i].f);
                    }
                    i++;
                }
                System.out.println(String.format("%.3f", sum));
            }
        }
        static class Food implements Comparable<Food>
        {
            public short j;
            public short f;
            
            public Food(short j,short f)
            {
                this.j=j;
                this.f=f;
            }
            @Override
            public int compareTo(Food o) {
                // TODO Auto-generated method stub
                if(this.j*1.0f/this.f>=o.j*1.0f/o.f)
                    return -1;
                else
                    return 1;
            }
        }
    }
  • 相关阅读:
    UML描述
    Tomcat优化
    Tomcat源码
    Tomcat架构
    搭建K8s集群[无需科学shangwang]
    minikube安装
    K8S核心组件和架构图
    Docker数据持久化
    Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式
    cookie与session区别?
  • 原文地址:https://www.cnblogs.com/maydow/p/4601042.html
Copyright © 2011-2022 走看看