将数组a中n个整数按相反顺序存放

1.

#include <stdio.h>
int main()
{
void inv(int x[],int n);
int i,a[10]={9,8,7,6,5,4,3,2,1,0};
printf("The original array:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
inv(a,10);
printf("The array has been inverted:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}

void inv(int x[],int n)
{
int temp,i,j,m=(n-1)/2;
for(i=0;i<=m;i++)
{
temp=x[i];
x[i]=x[n-1-i];
x[n-1-i]=temp;
}
}

2.

#include <stdio.h>
int main()
{
void inv(int *x,int n);
int i,a[10]={9,8,7,6,5,4,3,2,1,0};
printf("The original array:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
inv(a,10);
printf("The array has been inverted:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}

void inv(int *x,int n)
{
int *p,temp,*i,*j,m=(n-1)/2;
i=x;
j=x+n-1;
p=x+m;
for(;i<=p;i++,j--)
{
temp=*i;
*i=*j;
*j=temp;
}
}

3.

#include <stdio.h>
int main()
{
void inv(int *x,int n);
int i,arr[10],*p=arr;
printf("The original array:\n");
for(i=0;i<10;i++,p++)
scanf("%d",p);
printf("\n");
p=arr;
inv(p,10);
printf("The array has been inverted:\n");
for(p=arr;p<arr+10;p++)
printf("%d ",*p);
printf("\n");
return 0;
}

 

void inv(int *x,int n)
{
int *p,temp,*i,*j,m=(n-1)/2;
i=x;
j=x+n-1;
p=x+m;
for(;i<=p;i++,j--)
{
temp=*i;
*i=*j;
*j=temp;
}
}

 

posted @ 2019-01-08 16:38  莫攀缘  阅读(3430)  评论(0编辑  收藏  举报