class
Swap {
public
:
vector<
int
> getSwap(vector<
int
> num) {
num[
0
] = num[
1
]-num[
0
];
num[
1
] = num[
1
]-num[
0
];
num[
0
] = num[
0
]+num[
1
];
return
num;
}
};
a
|
b
|
a-b
|
返回值 |
+
|
+
|
+
|
a
|
-
|
-
|
+
|
a
|
+
|
+
|
-
|
b
|
-
|
-
|
-
|
b
|
+
|
-
|
+(溢出-)
|
a
|
-
|
+
|
-(溢出+)
|
b
|
class
Compare {
public
:
int
getSign(
int
num) {
return
((num >>
31
)&
1
)^
1
;
}
int
getMax(
int
a,
int
b) {
int
as = getSign(a);
int
bs = getSign(b);
int
cs = getSign(a-b);
int
dif = as^bs;
int
same = dif^
1
;
int
reta = same*cs+dif*as;
int
retb = reta^
1
;
return
reta*a+retb*b;
}
};
class
OddAppearance {
public
:
int
findOdd(vector<
int
> A,
int
n) {
int
res = A[
0
];
for
(
int
i =
1
;i < n;i++) {
res ^= A[i];
}
return
res;
}
};
推荐博文:
经典排序算法的C++实现