提交时间:2024-07-17 10:44:23

运行 ID: 156365

#include<iostream> #include<string> using namespace std; void bin(long long n, int end){ int i = 0; string tmp = ""; while(n){ if (n % end < 10){ tmp += (char)(n % end + '0'); } else{ tmp += (char)(n % end + 'A' - 10); } n /= end; i++; } for(int i = tmp.length() - 1; i >= 0; i--){ cout << tmp[i]; } cout << endl; } void res(long long n, int end){ int bin[1005], size = 0; do{ bin[++size] = n % end; n /= end; }while(n); long long bin_int = 0; for(int i = size; i >= 1; i--){ cout << bin[i]; } cout << endl; } int str_to_res(string n, int start){ long long number = 0, index_n = 1; for (int i = n.length() - 1; i >= 0; i--){ if(n[i] >= 'A' && n[i] <= 'F'){ number += (n[i] - 'A' + 10) * index_n; } else{ number += (n[i] - '0') * index_n; } index_n *= start; } return number; } int main(){ while(true){ int start, end; string n; long long i_n; cin >> start >> n >> end; i_n = str_to_res(n, start); if (end == 10){ cout << i_n << endl; } else if(end < 10){ res(i_n, end); } else{ bin(i_n, end); } } return 0; }