说明:代码为本人原创,若要引用,请注明出处。
#include<iostream>
#include<cctype>
using namespace std;
class NiXu
{
private:
int size;
int *nixu;
int *yuanxulie;
public:
NiXu();
void YuanXu();
~NiXu();
};
//--------------------------------------------------------------------------
NiXu::NiXu() //构造函数,输入逆序
{
cout<<"这个程序要求您输入一个排列的逆序序列。\n"
<<"\n例如排列p1 p2 p3 p4 p5 p6 p7 p8 "
<<"\n的逆序序列b1 b2 b3 b4 b5 b6 b7 b8是5 3 4 0 2 1 1 0。\n"
<<"则程序将算出原来的序列是4 8 6 2 5 1 3 7。\n";
cout<<"\n请输入该逆序序列的位数: ";
cin>>size;
cout<<"\n请输入逆序序列的各位: "<<endl;
nixu=new int[size];
int i;
for( i=0;i<size;i++)
{
cout<<"b"<<i+1<<" :";
cin>>nixu[i];
while((nixu[i]<0)||(nixu[i]>=size-i))
{
cout<<"Error! b"<<i+1<<" must between 0 and "<<size-1-i<<" !\n"
<<"b"<<i+1<<" :";
cin>>nixu[i];
}
}
cout<<"\n你输入的逆序序列是: ";
for(int j=0;j<size;j++)
{
cout<<nixu[j]<<" ";
}
cout<<"\n";
}
void NiXu::YuanXu() //求原序列
{
yuanxulie=new int[size+1];
int temp;
for(int k=size;k>0;k--)
{
int *pt=new int[size];
for(int t=0;t<size;t++)
{
pt[t]=yuanxulie[t];
}
temp=nixu[k-1];
yuanxulie[temp]=k;
for(int n=temp;n<size;n++)
{
yuanxulie[n+1]=pt[n];
}
delete [] pt;
}
cout<<"原来的序列是: ";
for(int m=0;m<size;m++)
{
cout<<yuanxulie[m]<<" ";
}
cout<<endl;
cout<<"Done!"<<endl;
delete [] yuanxulie;
}
NiXu::~NiXu()
{
delete [] nixu;
}
//-------------------------------------------------------------------
int main()
{
NiXu m;
m.YuanXu();
return 0;
}
本文地址:http://com.8s8s.com/it/it25788.htm