Advertisement
allia

системы счисления сдать

Nov 5th, 2020
2,721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. void perevod_1 (int n, int osn_2)
  7. {
  8.   int m = osn_2;
  9.   int c = 1;
  10.   int znach = n;
  11.   int result = 0;
  12.  
  13.   while (znach/m != 0)
  14.   {
  15.     m *= osn_2;
  16.     c++;
  17.   }
  18.  
  19.   int *arr = new int[c];
  20.  
  21.   for (int i=0; i<c; i++)
  22.   {
  23.     arr[i] = n % osn_2;
  24.     n /= osn_2;
  25.   }
  26.  
  27.   for (int i=c-1; i>=0; i--)
  28.     result += arr[i]*pow(10, i);
  29.      cout << result << " ";
  30.  
  31. }
  32.  
  33. void perevod_2 (int n, int osn_1, int osn_2)
  34. {
  35.   int c = 1;
  36.   int m = 10;
  37.   int znach = n;
  38.   int result;
  39.  
  40.   while (znach/m != 0)
  41.   {
  42.     m *= 10;
  43.     c++;
  44.   }
  45.  
  46.   int *arr = new int[c];
  47.  
  48.   for (int i=0; i<c; i++)
  49.   {
  50.     arr[i] = n % 10;
  51.     n /= osn_2;
  52.   }
  53.  
  54.   for (int i = c-1; i >= 0; i--)
  55.       result += arr[i]*pow(osn_1, i);
  56.  
  57.     cout << result << " ";
  58. }
  59.  
  60. int main()
  61. {
  62.   int n, osn_1, osn_2;
  63.   cin >> n >> osn_1 >> osn_2;
  64.   if (osn_2 < osn_1)
  65.   perevod_1(n, osn_2);
  66.   else perevod_2 (n, osn_1, osn_2);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement