-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumofTwoIntegers_371.cpp
More file actions
executable file
·60 lines (58 loc) · 1.35 KB
/
SumofTwoIntegers_371.cpp
File metadata and controls
executable file
·60 lines (58 loc) · 1.35 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
60
class Solution {
public:
int getSum(int a, int b){
int loop = 0;
int result = 0;
int caseNO = 0;
if (a >= 0 && b >= 0)
caseNO = 1;
else if (a < 0 && b >= 0)
caseNO = 2;
else if (a >= 0 && b < 0)
caseNO = 3;
else
caseNO = 4;
switch ( caseNO ){
case 1: // Note the colon, not a semicolon
if (a > b) {
result = a;
loop = b;
}
else{
result = b;
loop = a;
}
while (loop){
result++;
loop--;
}
break;
//a < 0 & b >= 0
case 2:
int counter = 0;
result = b;
while (a < 0)
{
counter++;
a++;
}
if (counter < b)
{
while (counter)
{
result--;
counter--;
}
}
break;
case 3: // Note the colon, not a semicolon
break;
case 4: // Note the colon, not a semicolon
break;
default: // Note the colon, not a semicolon
cout<<"Error, bad input, quitting\n";
break;
}
return result;
}
};