-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexspeak1271.cpp
More file actions
86 lines (80 loc) · 2.18 KB
/
Hexspeak1271.cpp
File metadata and controls
86 lines (80 loc) · 2.18 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
class Solution {
public:
void decToHexa(long long n, vector<char>& hexaDeciNum)
{
// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexaDeciNum.push_back(temp + 48);
i++;
}
else
{
hexaDeciNum.push_back(temp + 55);
i++;
}
n = n/16;
}
//reverse(hexaDeciNum.begin(), hexaDeciNum.end());
// printing hexadecimal number array in reverse order
/*for(int j=i-1; j>=0; j--)
cout << hexaDeciNum[j];*/
}
string toHexspeak(string num) {
vector<char>hexaDeciNum;
string ans;
decToHexa(stoll(num), hexaDeciNum);
for (int i = hexaDeciNum.size()-1; i > -1; i--) {
if (hexaDeciNum[i] == '1') {
ans+="I";
} else if (hexaDeciNum[i] == '0') {
ans+="O";
} else if (isalpha(hexaDeciNum[i])) {
ans+=hexaDeciNum[i];
} else {
return "ERROR";
}
}
return ans;
}
};
//the fatest solution
typedef long long Long;
typedef vector<int> vInt;
typedef vector<vInt> vvInt;
typedef vector<vvInt> vvvInt;
typedef vector<string> vStr;
typedef pair<int, int> Pair;
typedef vector<Pair> vPair;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define SZ(v) (int)v.size()
#define PB push_back
class Solution {
public:
string toHexspeak(string str) {
Long num = 0;
istringstream in(str);
in >> num;
string ans;
while (num > 0) {
int r = num % 16;
if (r > 1 && r < 10) return "ERROR";
if (r == 0) ans = "O" + ans;
else if (r == 1) ans = "I" + ans;
else {
ans = string(1, 'A' + r - 10) + ans;
}
num /= 16;
}
return ans;
}
};