三变量交换法:
#include <stdio.h>
int main()
{
int a,b,t;
scanf("%d%d",&a,&b);
t = a;
a = b;
b = t;
printf("%d %d\n" , a ,b );
return 0;
}不借助其他变量:
#include <stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
a = a + b;
b = a - b;
a = a - b;
printf("%d %d\n",a,b);
return 0;
}
本文介绍了两种在C语言中实现变量交换的方法:一种是使用第三个临时变量的传统方法;另一种是通过算术操作来完成交换,而不需要额外的存储空间。这两种方法为解决实际编程问题提供了灵活的选择。
1613

被折叠的 条评论
为什么被折叠?



