首页 技术 正文
技术 2022年11月15日
0 收藏 574 点赞 3,437 浏览 3547 个字

题目链接: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[];
int year;
int month;
int day;
int hour;
int minutes;
int seconds;
int ms;
double times;}log[];
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 = ;
while(gets(log[i].str))
{
if(strcmp(log[i].str,"")==)
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 = ;j<i;j++)
{
cout<<log[j].str <<endl;
}
return ;
}

运行时间: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[];
int year;
int month;
int day;
int hour;
int minutes;
int seconds;
int ms;
double times;}log[];
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?:-;
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 = ;
while(gets(log[i].str))
{
if(strcmp(log[i].str,"")==)
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[]),cmp);
//sort(log,log+i,cmp);
for(int j = ;j<i;j++)
{
cout<<log[j].str <<endl;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298