Advertisement
JosepRivaille

X32510: Codificació amb xifra 3

Apr 6th, 2015
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int base_3(int x) {
  6.     int aux;
  7.     int err;
  8.     err = x%3 +1;
  9.     aux = err;
  10.     x = x/3;
  11.     while (x != 0) {
  12.         err = x%3 + 1;
  13.         aux = 10*aux + err;
  14.         x = x/3;
  15.     }
  16.     return aux;
  17. }
  18.  
  19. //Pre: Llegeix un enter en decimal
  20. //Post: El codifica en base 3 (0 -> '-', 1 -> '+', 2 -> '*')
  21. int main() {
  22.     int x;
  23.     while (cin >> x) {
  24.         if (x < 0) {
  25.             x = -1*x;
  26.             cout << ':';
  27.         }
  28.         x = base_3(x);
  29.         while (x != 0) {
  30.             if (x%10 == 1) cout << '-';
  31.             else if (x%10 == 2) cout << '+';
  32.             else if (x%10 == 3) cout << '*';
  33.             x = x/10;
  34.         }
  35.         cout << endl;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement