CPP-负进制转换
负进制转换
class Solution {
public:
string baseNeg2(int n) {
//与正数的进制转换相类似,但需要保证余数是正数s
if(!n) return "0";
int chushu = -2;
string res;
while(n){
int t1 = n/chushu;
//需要保证余数是正数
if(n-t1*chushu<0){
t1++;
}
int t = n-t1*chushu;
n=t1;
res += '0' + t;
}
reverse(res.begin(),res.end());
return res;
}
};
本文来自博客园,作者:ghosteq,转载请注明原文链接:https://www.cnblogs.com/ghosteq/articles/16205384.html