小算法

重新看C语言时,一个小发现,在交换两个变量的值时,一般设个变量:

           int a  = 13, b = 27;

           int  c;

           c = a;

           a = b;

           b = c;

还可以不用中间变量:

           a = a + b;

           b = a - b;

           a = a - b;

    或:

          a = a ^ b;

          b = b ^ a;

                                                                        a = a ^ b;                                                          

                                                                                         //  ^为位异或运算符(异或运算符是直接对位进行运算的,所以运行结果是最快的)

你可能感兴趣的:(算法,职场,c/c++,休闲)