首页 > ACM题库 > HDU-杭电 > hdu 2115 I Love This Game-字符串处理-[解题报告]C++
2013
12-29

hdu 2115 I Love This Game-字符串处理-[解题报告]C++

I Love This Game

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

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

这道题目一看就知道是用结构体做,因为有两个限制条件,
一下是两个代码:(代码不同,是对字符串进行排序所用的方法不同)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>//用字符串string来比较大小一定要加这个头文件
using namespace std;
struct p
{
    string name;
    int  hour;
    int fen;
    int time;
    int paiming;
} f[11];

int cmp(p x, p y)
{
    if(x.hour==y.hour)
    {
        if(x.fen==y.fen)
        {
            //return strcmp(x.name,y.name)<0?1:0;
            return x.name<y.name;
        }
        else
            return x.fen<y.fen;
    }
    else
        return x.hour<y.hour;
}
int main()
{
    int n,i;
    int tcase=1;
    while(scanf("%d",&n),n)
    {
        for(i=0; i<n; i++)
        {
            cin>>f[i].name;
            scanf("%d:%d",&f[i].hour,&f[i].fen);
            f[i].time=60*f[i].hour+f[i].fen;
        }
        sort(f,f+n,cmp);
        f[0].paiming=1;
        for(i=1; i<n; i++)
        {
            if(f[i].time==f[i-1].time)
            {
                f[i].paiming=f[i-1].paiming;
            }
            else
                f[i].paiming=i+1;

        }
        if(tcase!=1)
        {
            printf("\n");
        }
        printf("Case #%d\n",tcase++);
        for(i=0; i<n; i++)
        {
            cout<<f[i].name<<" ";
            printf("%d\n",f[i].paiming);
        }
    }
    return 0;
}
二:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
struct p
{
    string name;
    int  hour;
    int fen;
    int time;
    int paiming;
} f[11];

int cmp(p x, p y)
{
    if(x.hour==y.hour)
    {
        if(x.fen==y.fen)
        {
            return strcmp(x.name,y.name)<0?1:0;//这里如果两个字符串相等的话就==0,如果第一个字符串比第二个字符串小==-1,如果第一个字符串比第二个字符串大==1;这是strcmp()函数
        }
        else
            return x.fen<y.fen;
    }
    else
        return x.hour<y.hour;
}
int main()
{
    int n,i;
    int tcase=1;
    while(scanf("%d",&n),n)
    {
        for(i=0; i<n; i++)
        {
            cin>>f[i].name;
            scanf("%d:%d",&f[i].hour,&f[i].fen);
            f[i].time=60*f[i].hour+f[i].fen;
        }
        sort(f,f+n,cmp);
        f[0].paiming=1;
        for(i=1; i<n; i++)
        {
            if(f[i].time==f[i-1].time)
            {
                f[i].paiming=f[i-1].paiming;
            }
            else
                f[i].paiming=i+1;

        }
        if(tcase!=1)
        {
            printf("\n");
        }
        printf("Case #%d\n",tcase++);
        for(i=0; i<n; i++)
        {
            cout<<f[i].name<<" ";
            printf("%d\n",f[i].paiming);
        }
    }
    return 0;
}

解题转自:http://blog.csdn.net/u010195743/article/details/9401119


  1. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。

  2. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。