HDOJ(HDU) 2115 I Love This Game(排序排序、、、)

324 篇文章 134 订阅
315 篇文章 4 订阅

Problem Description
Do you like playing basketball ? If you are , you may know the NBA Skills Challenge . It is the content of the basketball skills . It include several parts , such as passing , shooting , and so on. After completion of the content , the player who takes the shortest time will be the winner . Now give you their names and the time of finishing the competition , your task is to give out the rank of them ; please output their name and the rank, if they have the same time , the rank of them will be the same ,but you should output their names in lexicographic order.You may assume the names of the players are unique.

Is it a very simple problem for you? Please accept it in ten minutes.

Input
This problem contains multiple test cases! Ease test case contain a n(1<=n<=10) shows the number of players,then n lines will be given. Each line will contain the name of player and the time(mm:ss) of their finish.The end of the input will be indicated by an integer value of zero.

Output
The output format is shown as sample below.
Please output the rank of all players, the output format is shown as sample below;
Output a blank line between two cases.

Sample Input
10
Iverson 17:19
Bryant 07:03
Nash 09:33
Wade 07:03
Davies 11:13
Carter 14:28
Jordan 29:34
James 20:48
Parker 24:49
Kidd 26:46
0

Sample Output
Case #1
Bryant 1
Wade 1
Nash 3
Davies 4
Carter 5
Iverson 6
James 7
Parker 8
Kidd 9
Jordan 10

题目大意就是按照后面的时间排名,所用时间小的排前面,如果时间相等,按照名字的字典序排序。不会出现相同的名字。

用sort方法给它们排序好,再输出就行,注意,这里的难点是输出的时候,
排名相同的人,他们的名次必须另外用一个数来标志。

还有,输出的时候,2个输出之间有空行。

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t =0;
        while(sc.hasNext()){
            int n =sc.nextInt();
            if(n==0){
                break;
            }
            if(t!=0){
                System.out.println();
            }
            Stu stu[] = new Stu[n];
            for(int i=0;i<n;i++){
                stu[i] = new Stu();
                stu[i].name=sc.next();
                String str = sc.next();
                String strs[] = str.split(":");
                stu[i].h=Integer.parseInt(strs[0]);
                stu[i].m=Integer.parseInt(strs[1]);
                stu[i].d=i;//必须赋值不同的值
            }
            Arrays.sort(stu, new Comparator<Stu>() {
                @Override
                public int compare(Stu o1, Stu o2) {
                    if(o1.h>o2.h){
                        return 1;
                    }
                    if(o1.h<o2.h){
                        return -1;
                    }
                    if(o1.m>o2.m){
                        return 1;
                    }
                    if(o1.m<o2.m){
                        return -1;
                    }
                    //-1用来标识这个2个人的时间相等
                    o1.d=-1;
                    o2.d=-1;
                    return o1.name.toUpperCase().compareTo(o2.name.toUpperCase());
                }
            });

            System.out.println("Case #"+(++t));
            int h=1;//h是从1-n依次加一的数
            int k=1;//如果有重复排名时的排名
            int eq =1;//用来标识有几个重复的
            for(int i=0;i<n;i++){
                k=h-eq;//重复的时间有几次,就用h减去它,就是排名
                if(i==0){
                    System.out.println(stu[i].name+" "+(h++));
                }else{
                    if(stu[i].d==stu[i-1].d){//这个2个人的时间相等
                        eq++;
                        System.out.println(stu[i].name+" "+k);
                        h++;
                    }else{
                        eq=1;
                        System.out.println(stu[i].name+" "+(h++));
                    }
                }
            }
        }
    }
}

class Stu{
    String name;
    int h;
    int m;
    int d;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谙忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值