您的位置:首页 > 其它

磁盘文件最优存储问题

2015-11-22 22:19 351 查看
#include "iostream"
#include "fstream"
#include "queue"
using namespace std;

/*
将n个文件按概率排序,排序后有p1<=p2<=p3...<=pn
f(n-1)占中心磁道,f(n-2)和f(n-3)分居f(n-1)两侧,f(n-4)和f(n-5)分居最外两侧
*/

double p[50];

//返回n个文件的最小期望检索时间
//实际上p[i] = p[i]/(p[1]+p[2]+..+p
)
double greedy(int n)
{
sort(p, p+n);  //按概率排序
int i, j;
int *x = new int
;
int k = (n-1)/2;  //中心
x[k] = p[n-1];
for(i=k+1; i<n; i++)
x[i] = p[n-2*(i-k)];
for(i=k-1; i>=0; i--)
x[i] = p[n-2*(k-i)-1];

int P = 0;
double sum = 0;
for(i=0; i<n; i++)
{
P += p[i];
for(j=i+1; j<n; j++)
sum += x[i]*x[j]*(j-i);
}
return sum/P/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 >> p[i];
cout << p[i] << " ";
}

cout << "\n最小期望检索时间为:" << greedy(n);
cout << endl;
fin.close();
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: