编写函数Swap(int a[],int m,int n);实现数组前m个元素与后n个元素交换顺序(写的有点辣眼睛)
const int count=10;
void swap(int a[],int m,int n);
int main()
{
int a[count];
for(int i=0;i<count;i++)
{
cin>>a[i];
}
int first,last;
cout<<"please enter the loction where happen the swap'\n'";
cin>>first>>last;
swap(a,first,last);
return 0;
}
void swap(int a[],int m,int n)
{
int i,j;
for(i=0,j=count-n;i<m&&j<count;i++,j++)
{
int t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
int xx=i,yy=j;
while(xx<m)
{
int t2=a[i];
for(int z=i+1;z<count;z++)
a[z-1]=a[z];
a[count-1]=t2;
xx++;
}
while(yy<count)
{
int t3=a[yy];
for(int q=yy+1;q<count;q++)
{
a[q-1]=a[q];
}
for(int p=count-1;p>=i;p--)
{
a[p]=a[p-1];
}
a[i++]=t3;
yy++;
}
for(int d=0;d<count;d++)
{
cout<<a[d]<<'\t';
}
}
运行结果:
(1)
(2)