算法设计与分析之n次全排列和带期限的作业排序|上机实验实习报告|计算机上机实习报告含源代码
流程图请联系站长QQ752018766谢谢
//n次全排列
#include <stdio.h>
#include <stdlib.h>
int number[20], len, temp;
void output(){
int i;
printf("\n");
for(i=0; i<len; ++i)
printf("%d ", number[i]);
}
int pailie(int n){
int ii;
if(n==len)
output( );
for(ii=n; ii<len; ++ii) {
temp = number[ii]; number[ii] = number[n]; number[n] = temp;
pailie(n+1);
temp = number[ii]; number[ii] = number[n]; number[n] = temp;
}
return 0;
}
int main(int argc, char *argv[]){
int index = 0;
scanf("%d", &len);
while(index<len)
scanf("%d", &number[index++]);
pailie(0);
system("PAUSE");
return 0;
}
//带期限的作业排序问题
#include <iostream.h>
#include <iomanip.h>
void PX(int n,int *A);
void main()
{
int *A,*B;
int i,n;
cout<<"请输入作业数n:";
cin>>n;
A=new int[n+1];
B=new int[n+1];
cout<<"请输入作业i的期限值A(1-"<<n<<")"<<endl;
for(i=1;i<=n;i++)
{
cout<<"作业"<<i<<"的期限:";
cin>>A[i];
}
cout<<endl;
cout<<"请输入作业i的收益值B(1-"<<n<<")"<<endl;
for(i=1;i<=n;i++)
{
cout<<"作业"<<i<<"的收益:";
cin>>B[i];
}
cout<<endl;
PX(n,A);
}
void PX(int n,int *A)
{
int i,k,r;
int *C=new int[n+1];
k=1;
A[0]=0;
C[0]=0;
C[1]=1;
for(i=2;i<=n;i++)
{
r=k;
while(A[C[r]]>A[i] && A[C[r]]!=r)
r=r-1;
if(A[C[r]]<=A[i] && A[i]>r)
{
for(int x=k;x>=r+1;x--)
C[x+1]=C[x];
C[r+1]=i;
k++;
}
}
cout<<"作业的最优处理排序:";
for(i=1;i<=k;i++)
cout<<setw(4)<<C[i];
cout<<endl;
}