fork download
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. // convert number r-based to 10-based
  6. int convertAnyToDecimal(string number, int based) {
  7. int result = 0, exponent = 0;
  8.  
  9. for (int i = number.length() - 1; i >= 0 ; i--) {
  10. int numberParsed =
  11. ('0' <= number[i] && number[i] <= '9')
  12. ? number[i] - '0'
  13. : number[i] - 55;
  14. result += numberParsed * pow(based, exponent++);
  15. }
  16.  
  17. return result;
  18. }
  19.  
  20. // convert number 10-based to s-based
  21. string convertDecToAny(int numInDecimal, int basedTo) {
  22. string result = "";
  23. while (numInDecimal != 0) {
  24. result = to_string(numInDecimal % basedTo) + result;
  25. numInDecimal /= basedTo;
  26. }
  27. return result;
  28. }
  29.  
  30. // convert number r-based to s-based
  31. string convertNumber(string number, int based, int basedTo) {
  32. // convert number r-based to 10-based
  33. int numInDecimal;
  34. if (based != 10)
  35. numInDecimal = convertAnyToDecimal(number, based);
  36. else
  37. numInDecimal = stoi(number);
  38.  
  39. // convert number 10-based to s-based
  40. string result = "";
  41. if (basedTo != 10)
  42. result = convertDecToAny(numInDecimal, basedTo);
  43. else
  44. result = to_string(numInDecimal);
  45.  
  46. return result;
  47. }
  48.  
  49. int main() {
  50. int N;
  51. cin >> N;
  52. for (int i = 1; i <= N; i++) {
  53. string n; // <= 10^1000
  54. int r, s; // <= 36
  55. cin >> n;
  56. cin >> r >> s;
  57. string result = convertNumber(n, r, s);
  58. cout << result << endl;
  59. }
  60. return 0;
  61. }
Success #stdin #stdout 0s 15240KB
stdin
3
231 10 2
ABC 15 10
XYZ 36 2
stdout
11100111
2427
1010101111111011