#include<bits/stdc++.h>
using namespace std;
string func(int x){
string res;
if(x == 0){
res = "0";
}
while(x != 0){
int cur = x % 3;
if(cur == 0){
res += '0';
}
else if(cur == 1){
res += '1';
}
else if(cur == 2){
res += '-';
x += 3;
}
else if(cur == -1){
res += '-';
}
else if(cur == -2){
res += '1';
x -= 3;
}
x /= 3;
}
reverse(res.begin(), res.end());
return res;
}
int main(){
int n;
while(cin >> n){
string res = func(n);
cout << res << endl;
}
return 0;
}
对于正数:取余为0或1的时候是正常的,当取余为2时应该换为3-1,即在当前位置写入“-”,再将原数加3进行下一步操作
对于负数:取余为0时正常,取余为-1时在当前位置写入“-”,取余为-2时应换为-3+1,即在当前位置写入1,再将原数减3进行下一步操作

京公网安备 11010502036488号