Advertisement
DatProgrammer

X35210

Oct 22nd, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. //It DOES NOT convert to the proper base3 number,
  6. //it converts it to the reverse base3 number, such as:
  7. //2077 = 2211221 (Correct)
  8. //2077 = 1221122 (Reversed version)
  9.  
  10. int base_3(int n) {
  11.  
  12.   int z = n%3 + 1;
  13.   int aux = z;
  14.   n = n/3;
  15.   while (n != 0) {
  16.    
  17.     z = n%3 + 1;
  18.     aux = 10*aux + z;
  19.     n = n/3;
  20.   }
  21.   return aux;
  22. }
  23.  
  24. int main() {
  25.  
  26.   int n;
  27.   while (cin >> n) {
  28.    
  29.     if (n < 0) {          //Add ":" at the beginning if its negative.
  30.       n = -1*n;
  31.       cout << ':';
  32.     }
  33.    
  34.     n = base_3(n);        //Convert "n" to base 3
  35.      
  36.     while (n != 0) {
  37.    
  38.       if (n%10 == 1) cout << "-";               //Prints the correct symbol
  39.       else if (n%10 == 2) cout << "+";          //for each value
  40.       else if (n%10 == 3) cout << "*";
  41.       n = n/10;
  42.     }
  43.     cout << endl;
  44.    
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement