📄 noname01.cpp
字号:
#include<iostream.h>
#include<conio.h>
#include<process.h>
class tape
{
private:
int *c,n,*l,L,i,n;
public:
void getdata();
void process();
int *merge(int*,int,int,int);
int *mergesort(int*,int,int);
};
int *tape::merge(int *a,int low,int high,int mid)
{
int b[10],d[10];
int h,j,k;
h=low;
i=low;
j=mid+1;
while(h<=mid&&j<=high)
{
if(a[h]<=a[j])
{
b[i]=a[h];
d[i]=c[h];
h++;
}
else
{
b[i]=a[j];
d[i]=c[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
d[i]=d[k];
i++;
}}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
d[i]=c[k];
i++;
}
}
for(k=low;k<=high;k++)
{
a[k]=b[k];
c[k]=d[k];
}
return(a);
}
int* tape::mergesort(int *a,int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
return(a);
}
void tape::getdata()
{
cout<<"enter the lengh of tape: \t";
cin>>L;
cout<<"\n\n Enter the no. of program to be sorted on tape:\t";
cin>>n;
cout<<"\n";
l=new int(n);
c=new int(n);
for(int i=1;i<=n;i++)
{
cout<<"enter the lengh of program "<<i<<":\t";
cin>>l[i];
c[i]=i;
}
}
void tape::process()
{
int sum=0;
for(int i=1;i<=n;i++)
sum=sum+l[i];
cout<<"\n\nTotal lengh of all program is "<<sum<<endl;
if(sum>L)
{
cout<<"Program cannot be storage \n";
getch();
exit;
}
else
cout<<"therfore programs can be stored \n";
getch();
exit;
}
else
cout<<"Therfore programs can be stored on tape \n\n";
l=mergesort(l,l,n);
cout<<"The program are to be stored in stored in the order in the order :\t(";
for(i=1;i<=n;i++)
cout<<c[i]<<",";
cout<<"\b)\n";
cout<<"i.e according to lenghs:\t(";
for(i=1;i<=n;i++)
cout<<l[i]<<",";
cout<<"\b)\n";
int d=o;
for(int j=i;j<=n;j++)
d=d+l[i];
cout<<"\n\n The optimal sol is:<<d;
}
void main()
{
clrscr();
cout<<"\t\tOptimal storage on tape\n\n";
tape t;
t.getdata();
t.process();
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -