Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <vector>
- #include <string>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- bool less_10(char some){
- if (some=='0'||some=='1'||some=='2'||some=='3'||some=='4'||some=='5'||some=='6'||some=='7'||some=='8'||some=='9'){
- return true;
- } else{
- return false;
- }
- }
- int to_num(char a){
- int res;
- if (less_10(a)){
- res = a - 48;
- }else{
- res = a - 'A' + 10;
- }
- return res;
- //cout<< res<<'\n';
- }
- char to_char(ll x){
- char res;
- if (x < 10){
- res = x + 48;
- }else{
- res = x - 10 + 'A';
- }
- return res;
- //cout<<res<<'\n';
- }
- string to_str(int x){
- string res = to_string(x);
- return res;
- }
- ll helper(string num, int from){
- //1 -- перевести в десятичную
- string ans = ""; // в конце перевернём эту строку
- int sz = num.length();
- ll real = 0;
- for(int i = 1; i <= sz; ++i){
- char some = num[i-1]; //digit
- int dig;
- if (less_10(some)){
- dig = some - '0';
- }
- else{
- dig = to_num(some);
- }
- real += dig * pow(from, sz-i);
- }
- return real;
- }
- //2 -- перевести из десятичной в нужную систему
- string convert(ll tenth, int to){
- ll num = tenth;
- string S_num = to_string(num);
- string ans = "";
- while (num > 0){
- char sym; //symbol
- ll dig = num % to;
- sym = to_char(dig);
- //cout<<"dig = "<<dig<<'\n';
- ans += sym;
- num /= to;
- }
- reverse(ans.begin(), ans.end());
- return ans;
- }
- int main()
- {
- string num;
- int to, from;
- cin>>from>>num>>to;
- int tenth = helper(num,from);
- string result = convert(tenth, to);
- cout<<result<<'\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement