天天看点

HDOJ2093 考试排名考试排名View Code

考试排名

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 16041    Accepted Submission(s): 5604

Problem Description C++编程考试使用的实时提交系统,具有即时获得成绩排名的特点。它的功能是怎么实现的呢?

我们做好了题目的解答,提交之后,要么“AC”,要么错误,不管怎样错法,总是给你记上一笔,表明你曾经有过一次错误提交,因而当你一旦提交该题“AC”后,就要与你算一算帐了,总共该题错误提交了几回。虽然你在题数上,大步地跃上了一个台阶,但是在耗时上要摊上你共花去的时间。特别是,曾经有过的错误提交,每次都要摊上一定的单位时间分。这样一来,你在做出的题数上,可能领先别人很多,但是,在做出同样题数的人群中,你可能会在耗时上处于排名的劣势。

例如:某次考试一共8题(A,B,C,D,E,F,G,H),每个人做的题都在对应的题号下有个数量标记,负数表示该学生在该题上有过的错误提交次数,但到现在还没有AC,正数表示AC所耗的时间,如果正数a跟上一对括号,里面有个整数b,那就表示该学生提交该题AC了,耗去了时间a,同时,曾经错误提交了b次,因此对于下述输入数据:

HDOJ2093 考试排名考试排名View Code

若每次错误提交的罚分为20分,则其排名从高到低应该是这样的:

Josephus 5 376

John 4 284

Alice 4 352

Smith 3 167

Bob 2 325

Bush 0 0

Input 输入数据的第一行是考试题数n(1≤n≤12)以及单位罚分数m(10≤m≤20),每行数据描述一个学生的用户名(不多于10个字符的字串)以及对所有n道题的答题现状,其描述采用问题描述中的数量标记的格式,见上面的表格,提交次数总是小于100,AC所耗时间总是小于1000。

Output 将这些学生的考试现状,输出一个实时排名。实时排名显然先按AC题数的多少排,多的在前,再按时间分的多少排,少的在前,如果凑巧前两者都相等,则按名字的字典序排,小的在前。每个学生占一行,输出名字(10个字符宽),做出的题数(2个字符宽,右对齐)和时间分(4个字符宽,右对齐)。名字、题数和时间分相互之间有一个空格。

Sample Input

8 20
Smith	  -1	-16	8	0	0	120	39	0
John	  116	-2	11	0	0	82	55(1)	0
Josephus  72(3)	126	10	-3	0	47	21(2)	-2
Bush	  0	-1	-8	0	0	0	0	0
Alice	  -2	67(2)	13	-1	0	133	79(1)	-1
Bob	  0	0	57(5)	0	0	168	-7	0
        

Sample Output

Josephus    5  376
John        4  284
Alice       4  352
Smith       3  167
Bob         2  325
Bush        0    0
        

其实这个题目就是考察编程能力的,我的代码结果能出来,就是一直PE(Presentation Error)解决不了 --2017-11-20--

现在已经解决了,问题是这样的:

System.out.printf("%-10s %2d %4d\n", person.getName(), person.getAcNum(), person.getScore());

           

当我们把\n放到printf里面的时候,在eclipse/myeclipse是可以正确打印的 但是在HDOJ里面应该是不算数的,需要另外写一个换行的句子:

System.out.printf("%-10s %2d %4d", person.getName(), person.getAcNum(), person.getScore());
            System.out.println();
           

这样就不会PE了

题目比较长,看起来可能会有点吃力,大概是这样的: 输入的第一行:第一个是AC的题目总数,第二个是单位罚分 下面的就是:第一个是名字,之后的是AC题目个数或者用时情况 具体是: 负数表示:该学生在该题上有过的错误提交次数,但到现在还没有AC 整数表示:正数表示AC所耗的时间 括号里的数:曾经错误提交了b次

那么怎么来排名的呢? 首先是按照AC的题目总数,也就是正数的个数 然后是按照罚分总分来排,小的在前(正数加上括号里的数乘以单位罚分) 最后是名字

至于打印,题目没直接的提示,其实是放到while()外面的

View Code

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
    private static Scanner scanner;
    static List<Main.person> list;

    public static void main(String[] args) {
        scanner = new Scanner(System.in);
        int probrom = scanner.nextInt();// 题目数量
        int pubScore = scanner.nextInt();// 单位罚分
        list = new ArrayList<Main.person>();
        while (scanner.hasNext()) {
            int ac = 0;// ac题目个数
            int score = 0;// 罚分
            String name = "";// 姓名

            String strings[] = new String[probrom + 1];
            // 输入一个考生的信息
            // strings[0]是名字
            for (int i = 0; i < strings.length; i++) {
                strings[i] = scanner.next();
            }
            name = strings[0];
            // 计算AC题目个数
            for (int i = 1; i < strings.length; i++) {
                char ch = strings[i].charAt(0);
                if (ch >= '1' && ch <= '9') {
                    ac++;
                    try {
                        score += Integer.parseInt(strings[i]);
                    } catch (Exception e) {
                    }
                }
            }
            // 计算分数
            for (int i = 1; i < strings.length; i++) {
                // 第i个元素的 最后一个字符是否为')'// 也可以使用contains
                char ch = strings[i].charAt(strings[i].length() - 1);
                if (ch == ')') {
                    String[] split = strings[i].split("[(,)]");
                    score += pubScore * Integer.parseInt(split[1]) + Integer.parseInt(split[0]);
                }
            }
            Main p = new Main();
            person per = p.new person(ac, score, name);
            list.add(per);
        }
        listSort();
        for (person person : list) {
            System.out.printf("%-10s %2d %4d", person.getName(), person.getAcNum(), person.getScore());
            System.out.println();
        }
       
    }

    // 排序
    private static void listSort() {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = i + 1; j < list.size(); j++) {
                String iName = list.get(i).getName();
                String jName = list.get(j).getName();
                int iAc = list.get(i).getAcNum();
                int jAc = list.get(j).getAcNum();
                int iScore = list.get(i).getScore();
                int jScore = list.get(j).getScore();

                if (iAc < jAc) {
                    sort(i, j);
                } else if (iAc == jAc) {
                    if (iScore > jScore) {
                        sort(i, j);
                    } else if (iScore == jScore) {
                        int compareTo = iName.compareTo(jName);
                        if (compareTo > 0) {
                            sort(i, j);
                        }
                    }
                }
            }
        }
    }

    // 交换
    public static void sort(int i, int j) {
        int temp = list.get(i).getAcNum();
        list.get(i).setAcNum(list.get(j).acNum);
        list.get(j).setAcNum(temp);

        temp = list.get(i).getScore();
        list.get(i).setScore(list.get(j).score);
        list.get(j).setScore(temp);

        String string = list.get(i).getName();
        list.get(i).setName(list.get(j).getName());
        list.get(j).setName(string);
    }

    class person {
        int acNum;
        int score;
        String name;

        public person(int acNum, int score, String name) {
            super();
            this.acNum = acNum;
            this.score = score;
            this.name = name;
        }

        public void setAcNum(int acNum) {
            this.acNum = acNum;
        }

        public void setScore(int score) {
            this.score = score;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAcNum() {
            return acNum;
        }

        public int getScore() {
            return score;
        }

        public String getName() {
            return name;
        }

    }
}