Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int main(void)
  7. {
  8.  
  9. int n = 0;
  10.  
  11. cout <<"Enter n :";
  12. cin >> n;
  13.  
  14.  
  15. bool flag = false;
  16. if(n < 0)
  17. {
  18. n = -n;
  19. flag = true;
  20. }
  21.  
  22. int ost[2555];
  23.  
  24. char res[2555];
  25. int indexRes = 0;
  26.  
  27. int index = 0;
  28. int ostatok = 0;
  29.  
  30. while (n != 0)
  31. {
  32. ostatok = n % 3;
  33. if(ostatok == 2)
  34. {
  35. ostatok = -1;
  36. }
  37.  
  38. ost[index] = ostatok;
  39.  
  40. n = n / 3;
  41. index++;
  42. }
  43.  
  44. for(int i = index - 1; i>=0; i--)
  45. {
  46. if(ost[i] == -1)
  47. {
  48. res[indexRes] = '1';
  49. indexRes++;
  50. res[indexRes] = '$';
  51. indexRes++;
  52.  
  53.  
  54. }
  55. else if(ost[i] == 0)
  56. {
  57. res[indexRes] = '0';
  58. indexRes++;
  59. }
  60. else if(ost[i] == 1)
  61. {
  62. res[indexRes] = '1';
  63. indexRes++;
  64. }
  65. }
  66.  
  67.  
  68. if(flag == false)
  69. {
  70. for(int i= 0;i<indexRes;i++)
  71. {
  72. cout << res[i];
  73. }
  74. }
  75. else if(flag == true)
  76. {
  77. for(int i= 0;i<indexRes;i++)
  78. {
  79. if(res[i] == '$')
  80. {
  81. cout << '1';
  82. }
  83. else if(res[i] == '1')
  84. {
  85. cout << '$';
  86. }
  87. else cout << res[i];
  88.  
  89. }
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement