Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main(void)
- {
- int n = 0;
- cout <<"Enter n :";
- cin >> n;
- bool flag = false;
- if(n < 0)
- {
- n = -n;
- flag = true;
- }
- int ost[2555];
- char res[2555];
- int indexRes = 0;
- int index = 0;
- int ostatok = 0;
- while (n != 0)
- {
- ostatok = n % 3;
- if(ostatok == 2)
- {
- ostatok = -1;
- }
- ost[index] = ostatok;
- n = n / 3;
- index++;
- }
- for(int i = index - 1; i>=0; i--)
- {
- if(ost[i] == -1)
- {
- res[indexRes] = '1';
- indexRes++;
- res[indexRes] = '$';
- indexRes++;
- }
- else if(ost[i] == 0)
- {
- res[indexRes] = '0';
- indexRes++;
- }
- else if(ost[i] == 1)
- {
- res[indexRes] = '1';
- indexRes++;
- }
- }
- if(flag == false)
- {
- for(int i= 0;i<indexRes;i++)
- {
- cout << res[i];
- }
- }
- else if(flag == true)
- {
- for(int i= 0;i<indexRes;i++)
- {
- if(res[i] == '$')
- {
- cout << '1';
- }
- else if(res[i] == '1')
- {
- cout << '$';
- }
- else cout << res[i];
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement