# 不引入其它变量交换两数的值

^： 二进制异或运算符

#include

int main( )
{
unsigned int a=60;         //0011 1100
unsigned int b=13;         //0000 1101
printf("a=%d,b=%d",a,b);   //输出a，b的值
printf("\n");
a=a^b;                     //a=a^b=0011 0001
b=a^b;                     //b=a^b=0011 1100
a=a^b;                     //a=a^b=0000 1101
printf("a=%d,b=%d",a,b);   //输出a，b的值
}

#include <stdio.h>
#define MAX(x,y) ((x>y)?(x):(y))
#define SWAP1(x,y) {x=x+y;y=x-y;x=x-y;}
#define SWAP2(x,y) {x=x^y;y=x^y;x=x^y;}

int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("Max number is:%d\n",MAX(a,b));
printf("交换前：x=%d,y=%d\n",a,b);
SWAP1(a,b);
printf("交换后：x=%d,y=%d\n",a,b);
SWAP2(a,b);
printf("再次交换后：x=%d,y=%d\n",a,b);
return 0;
}

#include <stdio.h>

int main() {
int a, b;

a = 11;
b = 99;

printf("交换之前 - \n a = %d, b = %d \n\n", a, b);

a = a + b;  // ( 11 + 99 = 110)  此时 a 的变量为两数之和，b 未改变
b = a - b;  // ( 110 - 99 = 11)
a = a - b;  // ( 110 - 11 = 99)

printf("交换后 - \n a = %d, b = %d \n", a, b);
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120