zoukankan      html  css  js  c++  java
  • 两个变量的内容交换

     1 //最普通的变量交换
     2 //优点:思路简单
     3 //缺点:需要多余的存储空间 
     4 void swap1(int &a, int &b) 
     5 {
     6     int c;
     7     c=a;
     8     a=b;
     9     b=c;
    10 }
    11 
    12 //利用加减法的变量交换
    13 //优点:不需要多余的存储空间 
    14 //缺点:技巧性操作,不容易想到 
    15 void swap2(int &a, int &b) 
    16 {
    17     a=a+b;
    18     b=a-b;
    19     a=a-b; 
    20 }
    21 
    22 //利用位运算的变量交换
    23 //优点:不需要多余的存储空间 
    24 //缺点:技巧性操作,不容易想到 
    25 void swap3(int &a, int &b) 
    26 {
    27     a=a^b;
    28     b=a^b;
    29     a=a^b; 
    30 }
  • 相关阅读:
    CodeForces
    HDU
    HDU
    POJ
    URAL
    POJ
    UVa
    UVa
    UVa
    UVa
  • 原文地址:https://www.cnblogs.com/gremount/p/6735810.html
Copyright © 2011-2022 走看看