日志排序(gets函数与sscanf的使用)

题目链接:https://www.nowcoder.com/practice/0f64518fea254c0187ccf0ea05019672?tpId=40&tqId=21363&tPage=2&rp=2&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

题目描述

有一个网络日志,记录了网络中计算任务的执行情况,每个计算任务对应一条如下形式的日志记录: “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,消耗时间小数点后有三位数字。
计算任务名称与任务开始时间、消耗时间之间以一个或多个空格隔开,行首和行尾可能有多余的空格。

输出描述:

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

输入

复制
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)

代码
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Log{
    char str[100];
    int year;
    int month;
    int day;
    int hour;
    int minutes;
    int seconds;
    int ms;
    double times;
 
}log[10001];
bool cmp(Log L1,Log L2)
{
    if(L1.times!=L2.times)
        return L1.times<L2.times;
    else if(L1.year != L2.year)
        return L1.year < L2.year;
    else if(L1.month != L2.month)
        return L1.month < L2.month;
    else if(L1.day != L2.day)
        return L1.day < L2.day;
    else if(L1.hour != L2.hour)
        return L1.hour < L2.hour;
    else if(L1.minutes != L2.minutes)
        return L1.minutes < L2.minutes;
    else if(L1.seconds != L2.seconds)
        return L1.seconds < L2.seconds;
    else
        return L1.ms < L2.ms;

}
int main()
{
    int i = 0;
    while(gets(log[i].str))
    {
        if(strcmp(log[i].str,"")==0)
            break;
        sscanf(log[i].str,"%*s%4d-%2d%*c%2d %2d:%2d%*c%2d%*c%3d %lf%*s",&log[i].year,&log[i].month,&log[i].day,&log[i].hour,&log[i].minutes,&log[i].seconds,&log[i].ms,&log[i].times);
        
        i++;
    }
    cout<<i<<endl;
    sort(log,log+i,cmp);
    for(int j = 0;j<i;j++)
    {
        cout<<log[j].str <<endl;
    }
    return 0;
}

运行时间:13ms


占用内存:1388k

 

由于要记录前后中间的空格,所以用gets()函数会比较方便,同时利用sscanf  将数据提取;利用sort函数实现排序并重写cmp排序方法

或者使用qsort函数来实现

#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct Log{
    char str[100];
    int year;
    int month;
    int day;
    int hour;
    int minutes;
    int seconds;
    int ms;
    double times;
 
}log[10001];
int cmp(const void *a,const void *b)
{
    struct Log *L1 = (Log *)a;
    struct Log *L2 = (Log *)b;
    if(L1->times!=L2->times)
        return L1->times>L2->times?1:-1;
    else if(L1->year != L2->year)
        return L1->year - L2->year;
    else if(L1->month != L2->month)
        return L1->month - L2->month;
    else if(L1->day != L2->day)
        return L1->day - L2->day;
    else if(L1->hour != L2->hour)
        return L1->hour - L2->hour;
    else if(L1->minutes != L2->minutes)
        return L1->minutes - L2->minutes;
    else if(L1->seconds != L2->seconds)
        return L1->seconds - L2->seconds;
    else
        return L1->ms - L2->ms;

}
int main()
{
    int i = 0;
    while(gets(log[i].str))
    {
        if(strcmp(log[i].str,"")==0)
            break;
        sscanf(log[i].str,"%*s%4d-%2d%*c%2d %2d:%2d%*c%2d%*c%3d %lf%*s",&log[i].year,&log[i].month,&log[i].day,&log[i].hour,&log[i].minutes,&log[i].seconds,&log[i].ms,&log[i].times);
        
        i++;
    }
    qsort(log,i,sizeof(log[0]),cmp);
    //sort(log,log+i,cmp);
    for(int j = 0;j<i;j++)
    {
        cout<<log[j].str <<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ttzz/p/10471213.html