-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
60 lines (52 loc) · 1011 Bytes
/
main.cpp
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
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
string toHex(int num) {
char c[8] = {0};
for (int i = 7; i >= 0; i--) {
int digit = num & 0x0F;
if (digit < 10) {
c[i] = digit + '0';
} else {
c[i] = digit - 10 + 'a';
}
num = num >> 4;
}
bool first_digit = false;
string ret;
for (int i = 0; i < 8; i++) {
if (c[i] != '0') {
first_digit = true;
}
if (first_digit) {
ret.push_back(c[i]);
}
}
if (ret.size() > 0) {
return ret;
} else {
return "0";
}
}
};
int main(int argc, const char * argv[]) {
Solution sol;
int num;
num = 26;
cout << sol.toHex(num) << endl;
num = -1;
cout << sol.toHex(num) << endl;
num = 0;
cout << sol.toHex(num) << endl;
return 0;
}