将一个数组中的元素头尾两端依次对调

/*************************************************************************
    > File Name: myswap.c
    > Author: 
    > Mail: 
    > Created Time: Thu 31 Aug 2017 03:18:50 AM PDT
 ************************************************************************/
#include

void inplace_swap(int *x, int *y)
{
    *y = *x ^ *y;
    *x = *x ^ *y;
    *y = *x ^ *y;
}

void reverse_array(int a[], int cnt)
{
    int first, last;

    for(first = 0, last = cnt - 1; first <= last; first++, last--){
        inplace_swap(&a[first], &a[last]);
    }
}

int main(void)
{
    int i;
    int array[] = {14, 28, 40, 92, 95, 20, 13, 44, 70, 14};
    int length = sizeof(array) / sizeof(array[0]);
    
    reverse_array(array, length);

    for(i = 0; i < length; i++)
    {
        printf("%d ", array[i]);
    }
    printf("\n");
    
    return 0;
}

你可能感兴趣的:(将一个数组中的元素头尾两端依次对调)