-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimitive_types.cpp
More file actions
136 lines (112 loc) · 3.57 KB
/
primitive_types.cpp
File metadata and controls
136 lines (112 loc) · 3.57 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <random>
#include<bits/stdc++.h>
using namespace std;
// https://leetcode.com/problems/rectangle-area/submissions/
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int x1 = max(A, E);
int x2 = min(C, G);
int y1 = max(B, F);
int y2 = min(D, H);
int area = (x2-x1) * (y2-y1);
if(x1 > x2 || y1 > y2) area = 0;
return (C-A) * (D-B) - area + (G-E) * (H-F);
}
// https://leetcode.com/problems/rectangle-overlap/submissions/
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
int x1 = max(rec1[0], rec2[0]);
int x2 = min(rec1[2], rec2[2]);
int y1 = max(rec1[1], rec2[1]);
int y2 = min(rec1[3], rec2[3]);
return x1 < x2 && y1 < y2;
}
bool isPalindrome(int x) {
if(x < 0) return false;
if(x == 0) return true;
int nOfDigits = floor(log10(x));
int top = 1;
while(nOfDigits--) top *= 10;
while(x) {
if(x / top != x % 10) return false;
x = x % top / 10;
top /= 100;
}
return true;
}
// using folding
short parity(unsigned long long x) {
for(int i = 5; i >= 0; --i) x ^= (x >> (1 << i));
return x &= 1;
}
unsigned long long add(unsigned long long a, unsigned long long b) {
unsigned long long sum = 0, carryin = 0, k = 1, temp_a = a, temp_b = b;
while(temp_a || temp_b) {
int ak = a & k, bk = b & k;
unsigned long long carryout = (ak & bk) | (ak & carryin) | (bk & carryin);
sum |= (ak ^ bk ^ carryin);
carryin = carryout << 1, temp_a >>= 1, temp_b >>= 1, k <<= 1;
}
return sum | carryin;
}
unsigned long long multiply(unsigned long long x, unsigned long long y) {
unsigned long long sum = 0;
while(x) {
if(x & 0x1) sum = add(sum, y);
x >>= 1, y <<= 1;
}
return sum;
}
int concatNum(int d, int n) {
int ret = 0;
while(n) { ret *= 10, ret += d, --n; }
return ret;
}
bool checkConcatenatedSum(int num, int catlen) {
int tmpNum = num;
int sum = 0;
cout << num << endl;
while(tmpNum) {
sum += concatNum(tmpNum % 10, catlen), tmpNum /= 10;
cout << sum << endl;
}
cout << endl;
return sum == num;
}
void test() {
// cout << numeric_limits<float>::max() << endl;
// cout << numeric_limits<float>::min() << endl;
// default_random_engine engine;
// uniform_int_distribution<int> dist(1,6);
// cout << dist(engine) << endl;
// cout << dist(engine) << endl;
// cout << dist(engine) << endl;
// cout << dist(engine) << endl;
//
// uniform_int_distribution<unsigned long long> dist2(numeric_limits<unsigned long long>::min(), numeric_limits<unsigned long long>::max());
// cout << dist2(engine) << endl;
// cout << parity(4) << endl;
// cout << parity(5) << endl;
cout << multiply(10, 11) << endl;
cout << checkConcatenatedSum(198, 2) << endl;
cout << checkConcatenatedSum(2997, 3) << endl;
cout << checkConcatenatedSum(198, 3) << endl;
cout << checkConcatenatedSum(13332, 4) << endl;
cout << checkConcatenatedSum(9, 1) << endl;
}
class SolutionReverseInteger {
public:
int reverse(int x) {
bool neg = false;
if(neg) x *= -1, neg = true;
long long ret = 0;
while(x) {
ret = ret * 10 + x % 10;
x /= 10;
}
ret = (neg ? -1 : 1) * ret;
if(ret < numeric_limits<int>::min() || ret > numeric_limits<int>::max()) return 0;
return ret;
}
};
int main() {
test();
}