java入门--4110:圣诞老人的礼物-Santa Clau’s Gifts

学习了一下java的语法,就用poj上的题目做作练习,好更快的熟悉常用的java语法。

题目在这里 http://bailian.openjudge.cn/practice/4110/

import java.util.*;

public class Main {

    private static class Ware {
        public int value;
        public int count;
        public double v_p;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int rowcount = scan.nextInt();
        int weight = scan.nextInt();
        List<Ware>  wares= new ArrayList<Ware>();
        for(int i = 0; i < rowcount; ++i) {
            Ware w = new Ware();
            w.value = scan.nextInt();
            w.count = scan.nextInt();
            w.v_p = w.value * 1.0 /w.count;
            wares.add(w);
        }

        Collections.sort(wares, new Comparator<Ware>() {
            @Override
            public int compare(Ware o1, Ware o2) {
                if (Math.abs(o1.v_p - o2.v_p) < 1e-6) {
                    return 0;
                }
                else if (o1.v_p > o2.v_p)
                    return -1;
                else
                    return 1;
            }
        });

        double sum = 0;
        for (int i = 0; i < wares.size(); ++ i) {
            Ware w = wares.get(i);
            if (w.count <= weight) {
                sum += w.value;
            }
            else {
                sum += weight * w.v_p;
                break;
            }
            weight -= w.count;
        }
        System.out.printf("%.1f", sum);
    }
}

虽然简单,但是学习了java怎么处理输入;怎么排序;嵌套类默认会有一个外部类的引用,只能声明为static,才能在外部类的static函数中引用

posted on 2015-08-13 14:25  一颗麦粒  阅读(935)  评论(0编辑  收藏  举报

导航