有趣的變量交換方法

今天看到一個比較有趣的變量交換方法,雖然是很簡單,可能也派不上用場,也可以學習一下。

通常,變量交換通常是採用“三變量法”,代碼如下:

#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;
}

僅供娛樂一下。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章