这道题最难的点就是负进制(卡了我好久)我也讲不清楚,就不讲了
直接上代码
#include<bits/stdc++.h>
using namespace std;
int a,b;
stack<int> s;
char chan(int x)
{
if(x<10)
return x+'0';
else
return (x-10)+'A';//大于等于十的就输出A,B,C,D,E,FF
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(cin>>a>>b)
{
int k=a;
while(k!=0)
{
int c=k%b;
k/=b;
if(c<0&&b>0)
{
c+=b;
k--;
}
else if(c<0&&b<0)
{
c-=b;
k++;
}
s.push(c);
}
cout<<a<<'=';
while(!s.empty())
{
cout<<chan(s.top());
s.pop();
}
cout<<"(base"<<b<<")"<<"\n";
}
return 0;//功德圆满
}