-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65_Sum_WithNoOperation.cpp
More file actions
61 lines (47 loc) · 1.14 KB
/
65_Sum_WithNoOperation.cpp
File metadata and controls
61 lines (47 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Created by mark on 2019/7/30.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:65. 不用加减乘除做加法
2. 思路:位运算,分三步:
1. 不考虑进位每位相加:用异或代替
2. 两个1会产生进位,先做与运算,在左移
3. 把前两步结果相加 -> 重复前面两步,直到不产生进位即可
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
#include <list>
using namespace std;
int add(int num1, int num2)
{
int sum, carry;
do
{
sum = num1 ^ num2; // 1.计算每位相加
carry = (num1 & num2) << 1; // 2.计算进位
num1 = sum;
num2 = carry;
}while(num2 != 0); // 3.如果进位不为0,继续前两步
return num1;
}
int main()
{
cout << "输入相加的两个数:";
int num1,num2;
cin >> num1 >> num2;
cout << add(num1, num2) << endl;
return 0;
}