用栈写的:
#include<stdio.h>
#include<stack>
using namespace std;
stack<int>s;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
if(n<0)
{
printf("-");
n=-n;
}
while(n)
{
s.push(n%m);
n/=m;
}
while(!s.empty())
{
int e=s.top();
if(e<10)
printf("%d",e);
else
printf("%c",e+55);
s.pop();
}
printf("\n");
}
return 0;
}
或者:
#include<stdio.h>
int main()
{
int n,m,z[1007];
while(scanf("%d%d",&n,&m)==2)
{
int i=0;
if(n<0)
{
printf("-");
n=-n;
}
while(n!=0)
{
z[i++]=n%m;
n/=m;
}
for(int j=i-1;j>=0;j--)
{
if(z[j]<10)
printf("%d",z[j]);
if(z[j]>=10)
printf("%c",z[j]+55);
}
printf("\n");
}
return 0;
}