使用臨時(shí)變量交換兩個(gè)數(shù)的值
#include <stdio.h>
int main(){
//使用臨時(shí)變量召嘶;
int a,b,c;
printf("請(qǐng)輸入兩個(gè)數(shù):");
scanf("%d %d",&a,&b);
printf("使用臨時(shí)變量交換前:%d %d\n",a,b);
c = b;
b = a;
a = c;
printf("使用臨時(shí)變量交換后:%d %d\n",a,b);
return 0;
}
不使用臨時(shí)變量交換兩個(gè)數(shù)的值
#include <stdio.h>
int main(){
int a,b;
printf("請(qǐng)輸入兩個(gè)數(shù):");
scanf("%d %d",&a,&b);
printf("不使用臨時(shí)變量交換前:%d %d\n",a,b);
// 加入a=3(3的二進(jìn)制:011)夯膀,b=5(5的二進(jìn)制:101);
a = a^b; // 110 = 011 ^ 101
b = a^b; // 011 = 110 ^ 101
a = a^b; // 101 = 110 ^ 011
printf("不使用臨時(shí)變量交換后:%d %d\n",a,b);
return 0;
}