蜗牛漫步。。。我要一步一步往上爬。。。

看《深入理解计算机系统》时候看到的方法,,觉得很cool不需要第三个位置来临时存储另一个值来实现两个数值的交换void inplace_swap(int *a, int *b){*a = *a ^ *b;*b = *a ^ *b;*a = *a ^ *b;}利用上面的函数 ,实现将一个数组中的元素头尾两端依次对调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]);}}实现程序:#include <stdio.h>#include <stdlib.h>void inplace_swap(int *a, int *b);void reverse_array(int a[], int cnt);void main(void){int a[100];int i, len = 0;printf("输入数组元素:\n");for (i=0; i<100 && scanf("%d", &a[i])!=NULL; i++){len++;}reverse_array(a, len);printf("输出对调后的数组元素\n");for(i=0; i<len; i++){printf("%d ", a[i]);}system("pause");}void inplace_swap(int *a, int *b){*a = *a ^ *b;*b = *a ^ *b;*a = *a ^ *b;}//实现将一个数组中的元素头尾两端依次互调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]);}}

快乐时,想想我的影子,我会在云上为你喝彩

蜗牛漫步。。。我要一步一步往上爬。。。

相关文章:

你感兴趣的文章:

标签云: