不需要创建临时变量,交换两个数的值。

来源:互联网 发布:如何测试电信80端口 编辑:程序博客网 时间:2024/05/29 04:33
The easier way:#include<stdio.h>#include<stdlib.h>int main(){int a,b;scanf("%d%d",&a,&b);a=a+b;b=a-b;a=a-b;printf("a=%d\nb=%d\n",a,b);system("pause");return 0;}The solution is easy to deal with,otherwise it exist some weak points.(数据容易发生溢出)  The effective way:  #include<stdio.h>#include<stdlib.h>int main(){int a,b;scanf("%d%d",&a,&b);a=a^b;b=a^b;a=a^b;printf("a=%d\nb=%d\n",a,b);system("pause");return 0;}


本文出自 “liveyoung” 博客,转载请与作者联系!

0 0
原创粉丝点击