-
Notifications
You must be signed in to change notification settings - Fork 0
/
tape.cpp
90 lines (90 loc) · 1.92 KB
/
tape.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*************************************************************************
> File Name: tape.cpp
> Author:
> Mail:
> Created Time: 2017年04月25日 星期二 20时36分56秒
************************************************************************/
//磁带最优存储问题
//对于给定的n个文件,每个文件中有b个程序,他们长度分别是a,读取某个程序的时间为t =(1+2+....+n) a*b
//平均时间为总时间/程序总数目
#include<iostream>
#include<fstream>
using namespace std;
#define Max 100
static int b[Max] = {0};
typedef struct tape
{
int a;
int b;
}Tape;
void Sort(int n)
{
int i = 0;
int j = 0;
int k = 0;
int tmp = 0;
for(i = 0; i < n-1; i++)
{
k = i;//k标记最大元素下标
for(j = i + 1; j < n;j++)
{
if(b[j] > b[k])
{
k = j;
}
}
if(i != k)
{
tmp = b[k];
b[k] = b[i];
b[i] = tmp;
}
}
}
double Cal(Tape t[],int nn)
{
int i = 0;
double sum = 0.0;
double time = 0.0;
for(i = 0; i < nn; i++)
{
b[i] = t[i].a * t[i].b;
sum += t[i].b;
}
Sort(nn);
for(i = nn - 1; i >= 0; i--)
{
time += (i+1) * b[i];
}
return time / sum;
}
int main()
{
int i = 0;
int n = 0;
double avg = 0.0;
Tape tap[Max] = {0};
ifstream in("text1.txt");
if(in)
{
cout << "请输入有几个文件!" << endl;
in >> n;
cout << n << endl;
cout << "请输入程序长度和读取频数!" << endl;
for(i = 0; i < n; i++)
{
in >> tap[i].a >> tap[i].b;
cout << tap[i].a <<' '<< tap[i].b << endl;
}
in.close();
}
avg = Cal(tap,n);
ofstream out("text2.txt");
if(out)
{
out << avg;
out.close();
}
cout << "最优平均时间为:" << avg <<endl;
return 0;
}