您的位置:首页 > 其它

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

2015-10-20 21:03 253 查看
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” 博客,转载请与作者联系!
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: