-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAlienNumbers.cpp
More file actions
43 lines (30 loc) · 946 Bytes
/
AlienNumbers.cpp
File metadata and controls
43 lines (30 loc) · 946 Bytes
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
#include <cstdio>
#include <iostream>
#include <string>
long convertToDecimal(std::string number, std::string alphabet){
long base = alphabet.size();
long output = 0;
for(int p = 0; p < number.size(); p++){
output = base * output + alphabet.find(number[p]);
}
return output;
}
std::string convertFromDecimal(long number, std::string alphabet){
long base = alphabet.size();
std::string output = "";
if(number == 0){return std::string(1, alphabet[0]);}
while(number > 0){
output = alphabet[number % base] + output;
number /= base;
}
return output;
}
int main(){
size_t N; std::cin >> N;
for(int p = 1; p <= N; p++){
std::string number, source, target;
std::cin >> number >> source >> target;
std::cout << "Case #" << p << ": " << convertFromDecimal(convertToDecimal(number, source), target) << std::endl;
}
return 0;
}