zoukankan      html  css  js  c++  java
  • 九度OJ 1130:日志排序 (排序)

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:1265

    解决:303

    题目描述:

    有一个网络日志,记录了网络中计算任务的执行情况,每个计算任务对应一条如下形式的日志记录:
    “hs_10000_p”是计算任务的名称,
    “2007-01-17 19:22:53,315”是计算任务开始执行的时间“年-月-日 时:分:秒,毫秒”, 
    “253.035(s)”是计算任务消耗的时间(以秒计)
    hs_10000_p 2007-01-17 19:22:53,315 253.035(s)
    请你写一个程序,对日志中记录计算任务进行排序。
    时间消耗少的计算任务排在前面,时间消耗多的计算任务排在后面。
    如果两个计算任务消耗的时间相同,则将开始执行时间早的计算任务排在前面。

    输入:

    日志中每个记录是一个字符串,每个字符串占一行。最后一行为空行,表示日志结束。日志中最多可能有10000条记录。
    计算任务名称的长度不超过10,开始执行时间的格式是YYYY-MM-DD HH:MM:SS,MMM,消耗时间小数点后有三位数字。
    计算任务名称与任务开始时间、消耗时间之间以一个或多个空格隔开,行首和行尾可能有多余的空格。

    输出:

    排序好的日志记录。每个记录的字符串各占一行。
    输入的格式与输入保持一致,输入包括几个空格,你的输出中也应该包含同样多的空格。

    样例输入:
    hs_10000_p   2007-01-17 19:22:53,315     253.035(s)
    hs_10001_p   2007-01-17 19:22:53,315     253.846(s)
    hs_10002_m   2007-01-17 19:22:53,315     129.574(s)
    hs_10002_p   2007-01-17 19:22:53,315     262.531(s)
    hs_10003_m   2007-01-17 19:22:53,318     126.622(s)
    hs_10003_p   2007-01-17 19:22:53,318     136.962(s)
    hs_10005_m   2007-01-17 19:22:53,318     130.487(s)
    hs_10005_p   2007-01-17 19:22:53,318     253.035(s)
    hs_10006_m   2007-01-17 19:22:53,318     248.548(s)
    hs_10006_p   2007-01-17 19:25:23,367    3146.827(s)
    样例输出:
    hs_10003_m   2007-01-17 19:22:53,318     126.622(s)
    hs_10002_m   2007-01-17 19:22:53,315     129.574(s)
    hs_10005_m   2007-01-17 19:22:53,318     130.487(s)
    hs_10003_p   2007-01-17 19:22:53,318     136.962(s)
    hs_10006_m   2007-01-17 19:22:53,318     248.548(s)
    hs_10000_p   2007-01-17 19:22:53,315     253.035(s)
    hs_10005_p   2007-01-17 19:22:53,318     253.035(s)
    hs_10001_p   2007-01-17 19:22:53,315     253.846(s)
    hs_10002_p   2007-01-17 19:22:53,315     262.531(s)
    hs_10006_p   2007-01-17 19:25:23,367    3146.827(s)
    来源:
    2008年北京大学方正实验室计算机研究生机试真题

    思路:

    排序的条件较多,注意字符串处理的细节错误。


    代码:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    #define M 10000
     
    struct task {
        char str[100];
        char name[10+1];
        char date[2][20];
        double cost;
    };
     
    int compare(char *s1, char *s2, int i)
    {
        int a = atoi(s1+i);
        int b = atoi(s2+i);
        if (a == b)
            return 0;
        else
            return (a>b) ? 1 : -1;
    }
     
    int cmp(const void *a, const void *b)
    {
        struct task *x = (struct task *)a, *y = (struct task *)b;
        if (x->cost != y->cost)
            return (x->cost > y->cost) ? 1 : -1;
        int res;
        res = compare(x->date[0], y->date[0], 0);
        if (res)
            return res;
        res = compare(x->date[0], y->date[0], 5);
        if (res)
            return res;
        res = compare(x->date[0], y->date[0], 8);
        if (res)
            return res;
        res = compare(x->date[1], y->date[1], 0);
        if (res)
            return res;
        res = compare(x->date[1], y->date[1], 3);
        if (res)
            return res;
        res = compare(x->date[1], y->date[1], 6);
        if (res)
            return res;
        res = compare(x->date[1], y->date[1], 9);
        return res;
    }
     
    int main(void)
    {
        int i;
        char s[20];
        struct task t[M];
        int k;
     
        k = 0;
        while (gets(t[k].str))
        {
            sscanf(t[k].str, "%s%s%s%s", t[k].name, t[k].date[0], t[k].
    date[1], s);
            t[k].cost = atof(s);
            k++;
        }
     
        qsort(t, k, sizeof(t[0]), cmp);
     
        for (i=0; i<k; i++)
            printf("%s
    ", t[i].str);
     
        return 0;
    }
    /**************************************************************
        Problem: 1130
        User: liangrx06
        Language: C
        Result: Accepted
        Time:30 ms
        Memory:2564 kb
    ****************************************************************/


    编程算法爱好者。
  • 相关阅读:
    DES 加密算法
    socket编程之bind()函数
    如何启动ubuntu下的telnet服务
    基于duilib修改的版本上传了
    mmsPlayer, for android ,wince,windows,wm等
    [转]log4c 配置文件的用法
    mmsPlayer, for android ,wince,windows,wm等
    wince 版本的播放器 是基于 TC89系列
    cocos2dx做的一个圣诞节软件
    基于duilib修改的版本上传了
  • 原文地址:https://www.cnblogs.com/liangrx06/p/5083901.html
Copyright © 2011-2022 走看看