HDOJ(HDU) 2115 I Love This Game(排序排序、、、)
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;
}
HDOJ(HDU) 2115 I Love This Game(排序排序、、、)的更多相关文章
- HDOJ/HDU 2561 第二小整数(水题~排序~)
Problem Description 求n个整数中倒数第二小的数. 每一个整数都独立看成一个数,比如,有三个数分别是1,1,3,那么,第二小的数就是1. Input 输入包含多组测试数据. 输入的第 ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).1864 最大报销额 (贪心)
HDOJ(HDU).1864 最大报销额 题意分析 题目有点问题,原题中说的 单项物品的价值不得超过600元 应该是单类物品的价值不能超过600元. 一开始以为是01背包,后来按贪心写过了. 一张一张 ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- HDOJ(HDU).1015 Safecracker (DFS)
HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...
- HDOJ(HDU).1412 {A} + {B} (STL SET)
HDOJ(HDU).1412 {A} + {B} (STL SET) 点我挑战题目 题意分析 大水题,会了set直接用set即可. 利用的是set的互异性(同一元素有且仅有一项). #include ...
- HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)
HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值) 点我挑战题目 题意分析 从题目中可以看出是大数据的输入,和大量询问.基本操作有: 1.Q(i,j)代表求区间max(a ...
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...
随机推荐
- DOM - nodeType 的取值
DOM 中,共有 12 中不同类型的节点,nodeType 的取值以数值表示. 节点类型 描述 子节点 1 Element 表示元素. Element, Text, Comment, Proce ...
- 基于事件的异步模式——BackgroundWorker
实现异步处理的方法很多,经常用的有基于委托的方式,今天记录的是基于事件的异步模式.利用BackgroundWorker组件可以很轻松的实现异步处理,并且该组件还支持事件的取消.进度报告等功能.本文以计 ...
- 层模型--相对定位(position:relative)
如果想为元素设置层模型中的相对定位,需要设置position:relative(表示相对定位),它通过left.right.top.bottom属性确定元素在正常文档流中的偏移位置.相对定位完成的过程 ...
- python使用VBA:Excel创建图表(转)
# -*- coding: utf-8 -*- """ Created on Thu Mar 06 11:22:03 2014 @author: Administrato ...
- 入门3:PHP环境开发搭建(windows)
一.环境需要 硬件环境(最低配置): 双核CPU 8G内存 操作系统环境: Windows(64位)7+ Mac OS X 10.10+ Linux 64位(推荐Ubuntu 14 LTS) /**拓 ...
- python json string和dict的转化
__author__ = 'SRC_TJ_XiaoqingZhang' import json data1 = {'b': 789, 'c': 456, 'a': 123} encode_json = ...
- Android app自动化测试之Python+Appium环境搭建
1.安装JDK (1)JDK安装时会有两次,一次是jdk,第二次是jre. (2)环境变量配置: 添加JAVA_HOME变量, 值:Jdk的安装路径 添加CLASSPATH变量,值: .;%JAVA_ ...
- openerp import namespace
# If True, the Python modules inside the openerp namespace are made available# without the 'openerp. ...
- [原创]用python写了一个简单的markdown编辑器
以前我常用openoffice记录东西,最喜欢它的当然是在linux上能用了,还有里面的公式输入,前几天才了解markdown这个东东,初步了解发现它正是我需要的东西,可以用它随心所欲地记录些东西,而 ...
- C#面试-关于const和readonly(看了一个点赞很多的帖子有感而发!)
前景提要: 最近大家都在面试,讨论最多.最基础的问题,莫过于“关于const和readonly常见的笔试题剖析”,等等的大牛解析.我就是一个小菜,只不过,有点不敢苟同大牛的意见.废话少说,进入重点. ...