C#異或運算符的使用
題目描述
編寫一個控制臺應用,采用異或運算符,實現兩個整型變量值的交換。并在Program類的Main進行驗證。
輸入
依次輸入2個整數
輸出
輸出交換前、后兩個變量的值
樣例輸入
12 78
樣例輸出
before exchange first=12,second=78 after exchange first=78,second=12
using System;namespace ConsoleApp_A
{class Program{static void Main(string[] args){int a, b;bool ju1=int.TryParse(Console.ReadLine(),out a);bool ju2 = int.TryParse(Console.ReadLine(), out b);/*string s = Console.ReadLine();*/Console.WriteLine("before exchange first={0},second={1}", a, b);int c = a ^ b;a = c ^ a;b = c ^ b;Console.WriteLine("after exchange first={0},second={1}", a, b);}}
}
?