本文主要是介绍【九度】题目1130:日志排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目地址:http://ac.jobdu.com/problem.php?pid=1130
题目描述:
-
有一个网络日志,记录了网络中计算任务的执行情况,每个计算任务对应一条如下形式的日志记录:
“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年北京大学方正实验室计算机研究生机试真题
- 排序,难点在于时间比较大小。可以转为字符串比较。
- C++ AC
-
#include <iostream> #include<stdio.h> #include<string.h> #include<string> #include<algorithm> using namespace std; const int maxn = 10002; struct Task{char allMsg[200];char name[12];char startFir[15];char startSec[15];char wasteTime[15]; }tasks[maxn];bool cmp(Task t1,Task t2){int len1 = strlen(t1.wasteTime);int len2 = strlen(t2.wasteTime);if(len1 != len2){return len1 < len2;}else{int tmp = strcmp(t1.wasteTime , t2.wasteTime);if(tmp != 0){return tmp < 0;}else{tmp = strcmp(t1.startFir,t2.startFir);if(tmp != 0){return tmp < 0;}else{return strcmp(t1.startSec,t2.startSec) < 0;}}} } int main(){int k = 0;while(gets(tasks[k].allMsg)){sscanf(tasks[k].allMsg,"%s%s%s%s",tasks[k].name,tasks[k].startFir,tasks[k].startSec,tasks[k].wasteTime);k++;}sort(tasks,tasks + k,cmp);for(int i = 0; i < k; i++){cout<<tasks[i].allMsg<<endl;} return 0; } /**************************************************************Problem: 1130User: wangzhenqingLanguage: C++Result: AcceptedTime:130 msMemory:4040 kb ****************************************************************/
Java AC
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;public class Main {/* * 1130 */ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader( System.in ) ) ; List<ComputerTask> comTaskList = new ArrayList<ComputerTask>(); while (true) { String recordTask = br.readLine(); if (recordTask == null || "".equals(recordTask)) { break; } String newRecordTask = recordTask.trim().replaceAll("[' ']+", " "); String []array = newRecordTask.split(Pattern.quote(" ")); ComputerTask computerTask = new ComputerTask(array[0], (array[1]+" "+array[2]),array[3],recordTask); comTaskList.add(computerTask); } int size = comTaskList.size(); Collections.sort(comTaskList); StringBuffer sb = new StringBuffer(); for (int i = 0; i < size; i++) { sb.append(comTaskList.get(i).getAllMsg()+"\n"); } System.out.print(sb.toString()); }
}
class ComputerTask implements Comparable<ComputerTask>{ private String taskId; private String taskStartTime; private String useTime; private String allMsg; public String getTaskId() { return taskId; } public void setTaskId(String taskId) { this.taskId = taskId; } public String getTaskStartTime() { return taskStartTime; } public void setTaskStartTime(String taskStartTime) { this.taskStartTime = taskStartTime; } public String getUseTime() { return useTime; } public void setUseTime(String useTime) { this.useTime = useTime; } public String getAllMsg() { return allMsg; } public void setAllMsg(String allMsg) { this.allMsg = allMsg; } public ComputerTask(String taskId, String taskStartTime, String useTime, String allMsg) { super(); this.taskId = taskId; this.taskStartTime = taskStartTime; this.useTime = useTime; this.allMsg = allMsg; } public int compareTo(ComputerTask o) { int len1 = this.getUseTime().length();int len2 = o.getUseTime().length();if (len1 != len2) {return len1 - len2;}else {int tmp = this.getUseTime().compareTo(o.getUseTime());if (tmp != 0) {return tmp;}else {len1 = this.getTaskStartTime().length();len2 = o.getTaskStartTime().length();if (len1 != len2) {return len1 - len2;}else {return this.getTaskStartTime().compareTo(o.getTaskStartTime());}}}}
}/**************************************************************Problem: 1130User: wangzhenqingLanguage: JavaResult: AcceptedTime:1120 msMemory:101560 kb
****************************************************************/
这篇关于【九度】题目1130:日志排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!