题目链接:不用加减乘除做加法_牛客题霸_牛客网 (nowcoder.com)
思路如下图:
参考代码:
int Add(int num1, int num2) { while (num2 != 0) { int tmp = (num1 & num2) << 1; num1 ^= num2; num2 = tmp; } return num1; } int main() { int a = 0; int b = 0; scanf("%d %d", &a, &b); int ret = Add(a, b); printf("%d\n", ret); return 0; }