JosepRivaille

P53202: Números d'Azathoth

Apr 9th, 2015
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int canvi_base_2(int n) {
  7.     int b2 = 0;
  8.     int mult = 1;
  9.     while (n != 0) {
  10.         b2 = mult*(n%2) + b2;
  11.         n = n/2;
  12.         mult = mult * 10;
  13.     }
  14.     return b2;    
  15. }
  16.  
  17. void escriu_binari(int n) {
  18.     int digits = 1;
  19.     for (int i = n/50; i != 0; i = i/50) {
  20.         ++digits;
  21.     }
  22.     vector<int> azathoth(digits);
  23.     for (int i = digits-1; i >= 0; --i) {
  24.         azathoth[i] = n%50;
  25.         n = n/50;
  26.     }
  27.     for (int j = 0; j <= digits-1; ++j) {
  28.         int conv_bin = azathoth[j];
  29.         int bin = canvi_base_2(conv_bin);
  30.         cout << '.' << bin;
  31.     }
  32. }
  33.  
  34. //Pre: Llegeix un nombre natural
  35. //Post: Retorna la seqüència en binari a partir de la base 50
  36. int main() {
  37.     int n;
  38.     while (cin >> n) {
  39.         cout << n << " = ";
  40.         escriu_binari(n);
  41.         cout << '.';
  42.         cout << endl;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment