class Solution:
"""
@param a: The first integer
@param b: The second integer
@return: The sum of a and b
"""
def aplusb(self, a, b):
# write your code here, try to do it without arithmetic operators.
if a==0:return b
if b==0:return a
c = 1
d = 1
c = a^b
d = (a&b)<<1
return self.aplusb(c,d)
這樣寫,在處理[100,-100]時會顯示記憶體不夠之類的問題,不知道士什么原因
uj5u.com熱心網友回復:
while (b != 0) {
int _a = a ^ b;
int _b = (a & b) << 1;
a = _a;
b = _b;
}
return a;
uj5u.com熱心網友回復:
偶然看見一個c語言的,很精髓,貼出來
// addInt.cpp : 定義控制臺應用程式的入口點。
//
#include "stdafx.h"
int AddInt(int a, int b)
{
char *c = (char *)a;
return (int)&c[b];
}
int main()
{
int b = AddInt(1,4);
return 0;
}
uj5u.com熱心網友回復:
a+b可以分為三步來理解,比如a=3,b=21、a的二進制表示就是0011, b的二進制是0010,那么不考慮進位a+b的結果為0001。
2、 只考慮進位,結果就是0011+0010=0010
3、將該進位左移一位,變為00100
4、將00100再與0001進行異或,得到00101,即十進制下等于5
5、結束
uj5u.com熱心網友回復:
思路是一樣的,為什么python這個代碼沒辦法處理[-100,100],這組資料uj5u.com熱心網友回復:
試一下這個:a, b = map(int, input().split(" "))
print(a + b)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/19787.html
上一篇:pycharm配置問題
