日志排序(gets函数与sscanf的使用)
题目链接:https://www.nowcoder.com/practice/0f64518fea254c0187ccf0ea05019672?tpId=40&tqId=21363&tPage=2&rp=2&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking
题目描述
输入描述:
- 日志中每个记录是一个字符串,每个字符串占一行。最后一行为空行,表示日志结束。日志中最多可能有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)
- 代码
- #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 ;
- }
日志排序(gets函数与sscanf的使用)的更多相关文章
- Oracle学习总结_day03_day04_条件查询_排序_函数_子查询
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day03_条件查询_排序_函数 清空回收站: PUR ...
- 给 Memo 排序的函数
本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- SQL Server排名或排序的函数
SQL Server获得排名或排序的函数有如下几种: 1.Rank():在结果集中每一条记录所在的排名位置,但排名可能不连续,例如:若同一组内有两个第一名,则该组内下一个名次直接跳至第三名 sel ...
- 【SQL】四种排序开窗函数
一 .简单了解什么是开窗函数 什么是开窗函数,开窗函数有什么作用,特征是什么? 所谓开窗函数就是定义一个行为列,简单讲,就是在你查询的结果上,直接多出一列值(可以是聚合值或是排序号),特征就是带有ov ...
- 九度OJ 1130:日志排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1265 解决:303 题目描述: 有一个网络日志,记录了网络中计算任务的执行情况,每个计算任务对应一条如下形式的日志记录: "hs ...
- 九度oj 题目1130:日志排序
题目描述: 有一个网络日志,记录了网络中计算任务的执行情况,每个计算任务对应一条如下形式的日志记录:“hs_10000_p”是计算任务的名称,“2007-01-17 19:22:53,315”是计算任 ...
- C++日志操作开源函数库之Google-glog
今天想给我的C++项目找一个开源的日志类,用于记录系统日志,结果浪费了半个下午的时间.从网上搜索相关资料,找到以下几个备选方案: 1.log4cplus 下载地址:http://sourceforge ...
- list排序成员函数对string对象与char*对象排序的差别
对list容器中的对象排序,不能使用sort()算法,只能采用其自身的排序函数sort().因为,算法sort()只支持随机存取的容器的排序,如vector等. 对基本数据对象list排序:成员函数s ...
- C++对一组pair数据进行排序(sort函数的使用)
最近在写一个算法的时候,把一些数据存在了pair中,并且需要根据pair中first或者second的值对这些数据进行排序.比如:输入数据(1,2).(4,2).(3,3).(2,1)根据first的 ...
随机推荐
- Block 语法
Block,代码块,^符号是block的语法标记. 比如说,一个block的参数列表是一个UIView,返回值是个CGFloat,block名称是testBlock 可以定义为 CGFloat (^ ...
- js模板引擎初级
模板引擎:模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的html文档. 模板引擎的实现方式有 ...
- c 时间 学习
linux #include <stdio.h> #include <time.h> int main(int argc,char **argv) { //两种时间的获取方法 ...
- Android Studio 版本间区别
2.3.2 ->3.0.1 Gradle版本为4.1 com.android.tools.build:gradle:3.0.x Android Monitor 被换成了 Android P ...
- C# 图像处理:获取鼠标位置信息(全局)
Point ms = Control.MousePosition; //获取鼠标位置 this.label2.Text = string.Format("{0}:{1}", ms. ...
- C# 监测每个方法的执行次数和占用时间(测试2)
在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个 原文:http://www.cnblogs.com/RicCC/archive/2010/03/15 ...
- python脚本
python源码编译 python -O -m py_compile file.py [root@localhost python]# cat dbass.py #!/usr/local/bin/py ...
- pandas数据操作
pandas数据操作 字符串方法 Series对象在其str属性中配备了一组字符串处理方法,可以很容易的应用到数组中的每个元素 t = pd.Series(['a_b_c_d','c_d_e',np. ...
- Java的学习02
今天依旧记录一下今天的学习的知识. /** * 测试StringBuilder StringBuffer,可变字符序列 * String对象称为“不可变对象"指的是对象内部成员变量的值无法再 ...
- 第六篇:Jmeter Ftp服务器的连接
如上图: 创建一个---线程组----点击配置元件---添加FTP请求缺省值: IP:为你的FTP服务的IP remote file:为你FTP服务上的一个文件: Localfile:为你本地的路经: ...