异或运算

对于任意向量α,有α^α=0,利用此属性可做无中介变量值交换:

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

注意:这种做法并无性能上的提升,仅仅是一种智力上的游戏。

trick1:数组元素对调

没有中间变量,对调数组中首尾元素顺序。

void reverse_Array(int *a, int count){

  int first, last;

  for (first=0,last=count-1;first<last;first++,last--;){

    noTempSwap(&a[first],&a[last]);
  }

}

  

posted @ 2016-01-23 16:04  迈克儿  阅读(243)  评论(0编辑  收藏  举报