交換變數
使用第三方變數
1
2
3
4
5
6
7
8
9
int x = 10;
int y = 20;
// 開始交換
int temp;
temp = x;
x = y;
y = temp;
Console.WriteLine($"x:{x}, y:{y}");
不使用第三方的變數
1
2
3
4
5
6
7
8
int x = 10;
int y = 20;
//開始交換
x = x - y;
y = x + y;
x = y - x;
Console.WriteLine($"x:{x}, y:{y}");