zoukankan      html  css  js  c++  java
  • c# 交换两个变量

    使用临时变量:

    有人会问只使用两个变量交换,怎么办?

    不实用临时变量:

    第一种:

    a=a+b;
    b=a-b;
    a=a-b;

    第二种:

    异或:相同是0,不同是1

    上面是整型的,那么字符串可以直接异或吗?

    c#不行的.

    字符串类型:

    第一种(有临时变量)不好

                string af = "123", bf = "234";
                byte[] a = System.Text.Encoding.Unicode.GetBytes(af);
                byte[] b = System.Text.Encoding.Unicode.GetBytes(bf);
                byte[] rBytes = new byte[a.Length];
                for (int i = 0; i < a.Length; i++)
                {
                    a[i] = (byte)(a[i] ^ b[i]);
                }
                for (int i = 0; i < a.Length ; i++)
                {
                    b[i] = (byte)(b[i] ^ a[i]);
                }
                for (int i = 0; i < a.Length ; i++)
                {
                    a[i] = (byte)(a[i] ^ b[i]);
                }
                System.Text.UnicodeEncoding converter = new UnicodeEncoding();
                Console.WriteLine(converter.GetString(a) + "_" + converter.GetString(b));

    第二种:

    没临时变量

  • 相关阅读:
    c copy
    IfcVertexLoop
    qt windeployqt 日志
    IfcPolyLoop
    IfcEdgeLoop
    IfcLoop
    QTableWidget
    QList删除元素
    matlab X的负次方函数绘制2
    matlab X的负次方函数绘制1
  • 原文地址:https://www.cnblogs.com/handsomer/p/4551980.html
Copyright © 2011-2022 走看看