将一个数组中的值按逆序重新存放
#include <stdio.h>
int main()
{
int i,a[100],number,mid,temp1,temp2;
printf("How many numbers do you want to enter? \n");
scanf("%d",&number);//确定数组长度
printf("Please input array:\n");
for(i=0;i<number;i++)
scanf("%d",&a[i]);
printf("your array is:\n");
for(i=0;i<number;i++)
printf("%5d",a[i]);
printf("\n");
if(number%2==1)//输入奇数位序列
{
mid=(number-1)/2;
for(i=0;i<=mid;i++)
{
temp1=a[mid-i];
a[mid-i]=a[mid+i];
a[mid+i]=temp1;
}
}
else//输入偶数位序列
{
for(i=0;i<=number/2-1;i++)
{
temp2=a[i];
a[i]=a[number-(i+1)];
a[number-(i+1)]=temp2;
}
}
printf("逆序排序后的数组为:\n");
for(i=0;i<number;i++)
printf("%5d",a[i]);
return 0;
}