题解 | #进制转换#(栈)
进制转换
http://www.nowcoder.com/practice/2cc32b88fff94d7e8fd458b8c7b25ec1
class Solution {
public:
string solve(int M, int N) {
string ans;
string zm = "0123456789ABCDEF";
stack<char> st;
int flag = false;
if(M < 0) {M = -M; flag = true;}
while(M){
st.push(zm[M%N]);
M = M/N;
}
if(flag) st.push('-');
while(!st.empty()){
ans += st.top();
st.pop();
}
return ans;
}
};