百度2017暑期实习生编程题
来源:互联网 发布:网络运维书籍推荐 编辑:程序博客网 时间:2024/04/30 02:47
短作业优先(SJF, Shortest Job First)又称为“短进程优先”SPN(Shortest Process Next);是对FCFS算法的改进,其目标是减少平均周转时间。
短作业优先调度算法基于这样一种思想:
运行时间短的优先调度;
如果运行时间相同则调度最先发起请求的进程。
PS:本题题面描述有误,但原题如此,不宜修改,实际优先级如下:
1)接到任务的时间;
2) 如果接收时间相同则调度 运行时间最短的任务。
等待时间:一个进程从发起请求到开始执行的时间间隔。
现在有n个进程请求cpu,每个进程用一个二元组表示:(p,q),p代表该进程发起请求的时间,p代表需要占用cpu的时间。
请计算n个进程的平均等待时间。
输入描述:
输入包含多组测试数据。
对于每组测试数据,第一行为一个整数n。
然后有n行,每行两个整数,代表上述的二元组(p,q).
保证:
2<=n<=2000,1<=p<=300,1<=q<=100.
输出描述:
对于每组数据,输出一个浮点数,代表平均等待时间,请保留4位有效数字
输入例子:
4
1 4
1 3
1 5
2 1
输出例子:
5.2500
public class Main_1 { private static class Task implements Comparable{ public int id=0; public int length=0; public Task(int id, int length) { super(); this.id = id; this.length = length; } @Override public int compareTo(Object arg0) { Task task=(Task) arg0; if(this.id<task.id)return -1; else if(this.id>task.id)return 1; else{ if(this.length<task.length)return -1; else{ return 1; } } } public String toString(){ return id+" "+length; } } @SuppressWarnings("unchecked") public static void main(String[] args) { Scanner sc = new Scanner (new BufferedInputStream(System.in)); int m=sc.nextInt(); ArrayList<Task> array=new ArrayList<Task>(); for(int i=0;i<m;i++ ){ Task task=new Task(sc.nextInt(),sc.nextInt()); array.add(task); } Collections.sort(array); int start=1; double res=0; int temp=0; for(Task task:array){ if(task.id>=start){ start=task.id; temp=0; }else{ temp=start-task.id; } start=start+task.length; res=res+temp; } DecimalFormat df = new DecimalFormat("0.0000"); System.out.println(df.format(res / m)); }}
0 0
- 百度2017暑期实习生编程题
- 百度2017暑期实习生编程题
- 百度2017暑期实习生编程题
- 百度2017暑期实习生编程题
- 百度2017暑期实习生编程题-单词接龙
- 百度2017暑期实习生编程题-页面调度算法
- 百度2017暑期实习生编程题:单词接龙
- 页面调度算法----百度2017暑期实习生编程题
- 百度2017暑期实习生编程题单词接龙
- 成语接龙--百度2017暑期实习生编程题
- 百度2017暑期实习生编程罪犯转移
- 百度2017暑期实习生编程钓鱼比赛
- 百度2017暑期实习生编程裁减网格纸
- 乐视2017暑期实习生笔试编程题
- 腾讯2017暑期实习生编程题
- 腾讯2017暑期实习生编程题
- 腾讯2017暑期实习生编程题
- 腾讯2017暑期实习生编程题
- php自定义函数: 计算两个时间日期相隔的天数,时,分,秒
- 《TCP/IP详解 卷1:协议》读书笔记 第8章 Traceroute程序
- Error:Error: File path too long on Windows, keep below 240
- Unity中localPosition和position的区别
- 代码签名探析
- 百度2017暑期实习生编程题
- php本周、本月的第一天、最后一天的时间
- 数据分页的一些想法
- 【暑期第二次训练赛】小火山的围棋梦想
- Java 动态代理
- datagrid自定义分页控件
- jsp|java跨域取出网页指定元素htmlParer使用小例子
- 12-JSTL简介
- TextView文本显示图片和文字,竖直滚动显示