标签:交换两整数的值:加减 异或 乘除
(1)加减法:a=a+b;b=a-b;a=a-b;
(2)乘除法:a=a*b;b=a/b;a=a/b;
(3)异或运算:a=a^b;b=a^b;a=a^b;(遇到a可翻译b,遇到b可翻译a)
^异或(按位异或):相同出0,相异出1.
如:2 0000 0000 0000 0010
3 0000 0000 0000 0011
2^3 0000 0000 0000 0001 =1
2^3^2 0000 0000 0000 0011=3
--加减运算
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 0;
int b = 0;
scanf("%d", &a);
scanf("%d", &b);
a = a +b;
b = a-b;
a = a-b;
printf("%d\n",a);
printf("%d\n", b);
system("pause");
return 0;
}
--乘除运算
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 0;
int b = 0;
scanf("%d", &a);
scanf("%d", &b);
a = a * b;
b = a /b;
a = a / b;
printf("%d\n", a);
printf("%d\n", b);
system("pause");
return 0;
}
--异或运算
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 0;
int b = 0;
scanf("%d", &a);
scanf("%d", &b);
a = a^b;
b = a ^ b;
a = a ^ b;
printf("%d\n", a);
printf("%d\n", b);
system("pause");
return 0;
}
标签:交换两整数的值:加减 异或 乘除
原文地址:http://10741125.blog.51cto.com/10731125/1738061