zoukankan      html  css  js  c++  java
  • 一道簡單面試題:兩個整型不借助中間變量實現值互換

    一道簡單面試題:兩個整型不借助中間變量實現值互換的兩種算法
    C# Code:

    using System;
    namespace KiloNet
    {
        
    class test2 
        {
            
    static void Main(string[] args)
            {
                
    int a = int.MaxValue;
                
    int b =  int.MaxValue-2;
                Console.WriteLine(
    "a:{0},b:{1}",a,b);

                a 
    = b + (b = a) * 0;
                Console.WriteLine(
    "a:{0},b:{1}",a,b);
                
                a 
    = a^b;
                b 
    = a^b;
                a
    = b^a;
                Console.WriteLine(
    "a:{0},b:{1}",a,b);
                Console.Read();
            }
        }
    }
    //一道簡單面試題:兩個整型不借助中間變量實現值互換
    //java code 饅頭所有
    class test 
    {
        
    public static void main(String[] args) 
        {
            System.out.println(
    "Hello Kilo!");
            
    int a = 34;
            
    int b = 45;
            System.out.println(
    "a="++ ",b="+ b);

            a 
    = a^b;
            b 
    = a^b;
            a
    = b^a;
            System.out.println(
    "a="++ ",b="+ b);

            a 
    = a+b;
            b 
    = a-b;
            a
    = a-b;
            System.out.println(
    "a="++ ",b="+ b);

        }
    }

    //哈哈,這是Java2里看到.

    id 博主 = [[KILONET.CNBLOGS.COM alloc] initWithValue:@"天堂向右,我依然向左"

                  网名:@"老舟"

                  兴趣:@"影音,阅读"

                  动态:@"系统架构设计,Android通信模块开发"

                  网址:@"http://kilonet.cnblogs.com"
                  签名:@"--------------------------------------------------

                                  Stay Hungry , Stay Foolish

                                  求  知  若  渴,处  事  若  愚

                              --------------------------------------------------"

                  ];         // Never Release

  • 相关阅读:
    378. Kth Smallest Element in a Sorted Matrix
    387. First Unique Character in a String
    230. Kth Smallest Element in a BST
    384. Shuffle an Array(随机排序)
    454. 4Sum II
    627. Swap Salary
    166. Fraction to Recurring Decimal
    763. Partition Labels(贪心)
    496. Next Greater Element I
    766. Toeplitz Matrix
  • 原文地址:https://www.cnblogs.com/KiloNet/p/1554954.html
Copyright © 2011-2022 走看看