-2进制:用-2作为基数,(-2)^n作为权值来表示一个数。例如,7=1*(-2)^0+1*(-2)^1+0*(-2)^2+1*(-2)^3+1*(-2)^4,所以7用-2进制表示为11011。
将n转换成x进制代码:
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
int n,x; //将n转换成x进制
stack <int > st;
int r; //r为余数
while(cin>>n>>x)
{
while(!st.empty()) st.pop();
while(n)
{
r=n%x;
n/=x;
if(r<0)
{
r-=x;
n++;
}
st.push(r);
}
while(!st.empty())
{
printf("%d",st.top());
st.pop();
}
printf("\n");
}
return 0;
}