标题: 两数交换 不经过第三变量 [打印本页] 作者: 来明坤 时间: 2012-8-12 17:41 标题: 两数交换 不经过第三变量 class Program
{
static void Main(string[] args)
{
int a = 8, b = 2;
Program.exchange1(ref a, ref b);
Console.Write("a:{0},b:{1}", a, b);
}
private static void exchange1(ref int a, ref int b)
{
a = a + b;
b = a - b;
a = a - b;
}
private static void exchange2(ref int a, ref int b)
{
a = a - b;
b = a + b;
a = b - a;
}
private static void exchange3(ref int a, ref int b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
} 作者: yl89898 时间: 2012-8-12 18:04
学习了,真不错作者: 许庭洲 时间: 2012-8-13 08:28
太棒了,值得学习!