zoukankan      html  css  js  c++  java
  • 1. A + B Problem【easy】

    Write a function that add two numbers A and B. You should not use + or any arithmetic operators.

     Notice

    There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and return it.

    Clarification

    Are a and b both 32-bit integers?

    • Yes.

    Can I use bit operation?

    • Sure you can.
    Example

    Given a=1 and b=2 return 3

    Challenge 

    Of course you can just return a + b to get accepted. But Can you challenge not do it like that?

    解法一:

     1 class Solution {
     2 public:
     3     /*
     4      * @param a: The first integer
     5      * @param b: The second integer
     6      * @return: The sum of a and b
     7      */
     8     int aplusb(int a, int b) {
     9         while (b != 0) {
    10             int aa = a ^ b;
    11             int bb = (a & b) << 1;
    12             a = aa;
    13             b = bb;
    14         }
    15         
    16         return a;
    17     }
    18 };

     

  • 相关阅读:
    fiddler抓取java系程序的网络通信
    ZOJ 2724 Windows Message Queue(优先队列)
    FZU 电动车通行证制度
    Havel定理
    Catch That Cow
    Trie树
    zoj 2876 Phone List
    zoj 2420
    getchar
    zoj 1315 Excuses, Excuses!
  • 原文地址:https://www.cnblogs.com/abc-begin/p/8409893.html
Copyright © 2011-2022 走看看