您的位置:首页 > 其它

三种方法实现值互换

2015-06-23 14:07 253 查看
//
//  main.c
//
//  Created by Northcity on 15-6-23.
//  Copyright (c) 2015年 tcg. All rights reserved.
//
#include <stdio.h>

//传统方法互换
void switch1(int *a,int *b){
    
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
    
}

//switch2  switch3方法原理基本一样
void switch2(int *a,int *b){
    *a=*a+*b;
    *b=*a-*b;
    *a=*a-*b;
}
void switch3(int *a,int *b){

    *a=*a^*b;
    *b=*a^*b;
    *a=*a^*b;
}

int main(int argc, const char * argv[])
{
    int x,y;
    scanf("%d %d",&x,&y);
//    switch1(&x, &y);
//    switch2(&x, &y);
    switch3(&x, &y);
    printf("%d %d\n",x,y);
    
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: