磁带最优存储问题

来源:互联网 发布:java 进程间通信 编辑:程序博客网 时间:2024/05/17 05:09
#include "iostream"#include "fstream"#include "queue"using namespace std;/*最短平均读取时间优先 */double p[50];int l[50];int y[50];  //存放每个程序的平均读取时间//返回n个程序的最小平均读取时间//实际上p[i] = p[i]/(p[1]+p[2]+..+p[n])double greedy(int n){    int i;    for(i=0; i<n; i++)        y[i] = p[i] * l[i];    sort(y, y+n);    for(i=1; i<n; i++) //计算每个程序的平均读取时间        y[i] += y[i-1];    int time = 0;  //所有程序平均读取时间总和    double P = 0;    for(i=0; i<n; i++)    {        time += y[i];        P += p[i];    }    return time / P;}int main(){    ifstream fin("最优存储.txt");    int n;    cout << "输入程序个数:";    fin >> n; cout << n;    cout << "\n输入各程序的长度和读取概率:\n";    int i;    for(i=0; i<n; i++)    {        fin >> l![\[](http://img.blog.csdn.net/20151122213441764)i] >> p[i];        cout << l[i] << " " << p[i] << endl;    }    cout << "\n最小平均读取时间为:" << greedy(n);    cout << endl;    fin.close();    return 0;} 

这里写图片描述

0 0