题目:http://acm.hdu.edu.cn/showproblem.php?pid=2115
有那位仁兄是做过这道题的,能否帮忙解释下你的思路。
我的解题思路:
1.输入整数,判断是否为0,为0则退出。
2.判断该整数是否大于1,大于1,即输出一个空行。
3.输入N个players和其所以使用的Time,保存在TreeMap里面
4.又因题目说name是唯一的,所以TreeMap里面key=name,value=time;
5.再根据TreeMap里面的值来排序。
6.然后是按格式输出:name+空格+rank
用的测试用例:
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
Case #1
Bryant 1
Wade 1
Nash 3
Davies 4
Carter 5
Iverson 6
James 7
Parker 8
Kidd 9
Jordan 10
2Jordan 29:34
James 20:48
Case #2
James 1
Jordan 2
1jo 12:00
Case #3
jo 1
问:这样的输出格式对不?package com.nt;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;/**
 * Hdu 2115 I Love this game
 * <p>题目:<a href=http://acm.hdu.edu.cn/showproblem.php?pid=2115>
 * http://acm.hdu.edu.cn/showproblem.php?pid=2115</a></p>
 * @author ps
 */
public class Hdu2115 { /**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
int input = 0;
while((str = br.readLine()) != null){
//输入为0时退出循环
if(str.equals("0")){
break;
}
input ++;//用来标识case #1等。
if(input > 1){
System.out.println();
}
int n = Integer.parseInt(str);
Map map = new TreeMap();
for(int i=0;i<n;i++){
str = br.readLine();

String []list = str.split(" ");
String name = list[0];
String time = list[1];

map.put(name, time);
}
//使用TreeMap的value的升序来排序。
Set s = map.entrySet();
Map.Entry []datas = (Map.Entry[])s.toArray(new Map.Entry[s.size()]);
Arrays.sort(datas, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
String key1 = ((Map.Entry)o1).getValue().toString();
String key2 = ((Map.Entry)o2).getValue().toString();
return key1.compareTo(key2);
}

});
System.out.println("Case #"+input);
//按排名输出
int newRank = 1;
int oldRank = 1;
int count = 0;
String newStr = "";
String oldStr = "";
if(datas.length>0){
oldStr = (String)datas[0].getValue();
}
boolean flag = false;
for(int j=0;j<datas.length;j++){
newStr = (String) datas[j].getValue();
if(newStr.equals(oldStr)){
newRank = oldRank;
count ++;
flag = true;
}else{
if(flag){
newRank += count;
count = 0;
flag = false;
}else{
newRank ++;
}
oldStr = newStr;
oldRank = newRank;
}
System.out.print(datas[j].getKey()+" ");
System.out.println(newRank);
}
}
}}

解决方案 »

  1.   

    这个题目我做过,不过是用C++的,
    一组测试中可能有多个人的时间相同,LZ在这方面可能没处理好附上我已经AC了的代码吧,虽然是C++的,LZ就参考一下吧,呵呵。
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    struct Player
    {
        string name;
        int time;
    };
    bool cmp(Player a, Player b)
    {    return a.time < b.time;        }int main()
    {
        int count, n, i, min, sec, ts;
        Player temp;
        bool first = true;
        vector<Player> vec;
        count = 1;
        while(cin >> n && n)
        {
            for(i=0;i<n;i++)
            {
                cin >> temp.name;
                cin >> min;
                getchar();
                cin >> sec;
                temp.time = min * 60 + sec;
                vec.push_back(temp);
            }
            sort(vec.begin(), vec.end(), cmp);
            ts = 0;
            if(first)
                first = false;
            else
                cout << endl;
            cout << "Case #" << count++ << endl;
            cout << vec[0].name << " " << "1\n";
            for(i=1;i<n;i++)
            {
                cout << vec[i].name << " ";
                if(vec[i].time == vec[i-1].time)
                    cout << ts+1 << endl;
                else
                {
                    cout << i+1 << endl;
                    ts = i;
                }
            }
            vec.clear();
        }
        return 0;
    }
      

  2.   

    if they have the same time , the rank of them will be the same ,but you should output their names in lexicographic order
    对应的代码呢?
      

  3.   


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;/**
     * Hdu 2115 I Love this game
     * nihuajie.info
     * 
     * @modified by 倪华杰
     */
    public class Hdu2115 {    /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String str = null;
            int input = 0;
            while((str = br.readLine()) != null){
                //输入为0时退出循环
                if(str.equals("0")){
                    break;
                }
                input ++;//用来标识case #1等。
                if(input > 1){
                    System.out.println();
                }
                int n = Integer.parseInt(str);
                Map map = new TreeMap();
                for(int i=0;i<n;i++){
                    str = br.readLine();
                    
                    String []list = str.split(" ");
                    String name = list[0];
                    String time = list[1];
                    
                    map.put(name, time);
                }
                //使用TreeMap的value的升序来排序。
                Set s = map.entrySet();
                Map.Entry []datas = (Map.Entry[])s.toArray(new Map.Entry[s.size()]);
                Arrays.sort(datas, new Comparator(){
                    @Override
                    public int compare(Object o1, Object o2) {
                        String key1 = ((Map.Entry)o1).getValue().toString();
                        String key2 = ((Map.Entry)o2).getValue().toString();
                        if(key1.equals(key2))
                        {
                         return ((Map.Entry)o1).getKey().toString().compareTo(((Map.Entry)o2).getKey().toString());
                        }
                        else
                        return key1.compareTo(key2);
                    }
                    
                });
                System.out.println("Case #"+input);
                //按排名输出
                int someting=1;
                for(int index=0;index<datas.length;index++)
                {
                 if(index==0||!datas[index].getValue().toString().equals(datas[index-1].getValue().toString()))
                 someting=index+1;
                 System.out.println(datas[index].getKey().toString()+"   "+someting);
                }
            }
        }}
    在你的代码基础上改的。
    自己看吧