标签:
将M进制的数X转换为N进制的数输出。
输入的第一行包括两个整数:M和N(2<=M,N<=36)。
下面的一行输入一个数X,X是M进制的数,现在要求你将M进制的数X转换成N进制的数输出。
输出X的N进制表示的数。
16 10 F
15
输入时字母部分为大写,输出时为小写,并且有大数据。
#include <stdio.h>
#include <string.h>
#define maxsize 1000
struct
biginteger
{
int
size;
int
digit[maxsize];
void
init()
{
memset
(digit,0,
sizeof
(
int
)*maxsize);
size=0;
}
void
setint(
int
in)
{
do
{
digit[size++]=in%10000;
in=in/10000;
}
while
(in!=0);
}
void
output()
{
for
(
int
i=size-1; i>=0; i--)
printf
(
"%d"
,digit[i]);
printf
(
"\n"
);
}
biginteger operator * (
int
x)
const
{
biginteger ret;
ret.init();
int
carry=0;
for
(
int
i=0; i<size; i++)
{
int
tmp=digit[i]*x+carry;
carry=tmp/10000;
ret.digit[ret.size++]=tmp%10000;
}
if
(carry!=0)
{
ret.digit[ret.size++]=carry;
}
return
ret;
}
biginteger operator + (
const
biginteger &A)
const
{
biginteger ret;
ret.init();
int
carry=0;
for
(
int
i=0; i<size||i<A.size; i++)
{
int
tmp = A.digit[i] + digit[i] + carry;
carry = tmp /10000;
ret.digit[ret.size++]=tmp%10000;
}
if
(carry!=0)
{
ret.digit[ret.size++]=carry;
}
return
ret;
}
biginteger operator /(
int
x)
const
{
//here suppose x < 10000
biginteger ret;
ret.init();
int
remainder = 0;
for
(
int
i=size-1; i>=0; i--)
{
int
tmp = (remainder*10000+digit[i])/x;
int
tmp2 = (remainder*10000+digit[i])%x;
ret.digit[i]=tmp;
remainder = tmp2;
}
for
(
int
i=0; i<maxsize; i++)
{
if
(ret.digit[i]!=0) ret.size=i;
}
ret.size+=1;
return
ret;
}
int
operator %(
int
x)
const
{
int
remainder = 0;
for
(
int
i=size; i>=0; i--)
{
int
tmp=(remainder*10000+digit[i])%x;
remainder = tmp;
}
return
remainder;
}
} a,b;
//a:input b:output c:decimal
char
inputstr[maxsize*maxsize];
char
ans[maxsize*maxsize];
int
inh,outh;
int
strsize;
int
main()
{
#ifdef LOCAL
freopen
(
"data.in"
,
"r"
,stdin);
freopen
(
"data2.out"
,
"w+"
,stdout);
#endif // LOCAL
//Format input here
while
(
scanf
(
"%d%d"
,&inh,&outh)!=EOF)
{
//Do something here
scanf
(
"%s"
,inputstr);
strsize=
strlen
(inputstr);
a.init();
b.init();
a.setint(0);
b.setint(1);
for
(
int
i=strsize-1; i>=0; i--)
{
//从低位开始freopen("data1.out","w+",stdin);
int
tmp = 0;
if
(inputstr[i]>=
‘0‘
&&inputstr[i]<=
‘9‘
)
tmp = inputstr[i]-
‘0‘
;
else
if
(inputstr[i]>=
‘A‘
&&inputstr[i]<=
‘Z‘
)
{
tmp= inputstr[i]-
‘A‘
+10;
}
a=a+b*tmp;
b=b*inh;
}
//a.output();
//除法求outh进制
int
ptr = 0;
//指向ans中的位置
do
{
int
rem=a%outh;
if
(rem>=10)ans[ptr++]= rem-10+
‘a‘
;
else
ans[ptr++]=rem+
‘0‘
;
a=a/outh;
}
while
(a.digit[0]!=0||a.size!=1);
//这里需要特别分清a的用法,如果跟的是上面重载过的运算符就可以直接赋值,如果不是就得按照数组的一般进行。
for
(
int
i=ptr-1;i>=0;i--){
printf
(
"%c"
,ans[i]);
}
printf
(
"\n"
);
}
return
0;
}
/**************************************************************
Problem: 1080
User: windcarp
Language: C++
Result: Accepted
Time:160 ms
Memory:2980 kb
****************************************************************/
#include<stdio.h>
char s[1000],t;
int X[1000],Y[1000];
inline int ctoi(char c)
{
if(c<=‘9‘)return c-‘0‘;
else return c-‘A‘+10;
}
inline char itoc(int i)
{
if(i<10)return i+‘0‘;
else return i-10+‘a‘;
}
int main()
{
freopen("data.in","r",stdin);
freopen("data1.out","w+",stdout);
int M,N,i,j,l;
while(~scanf("%d%d%s",&M,&N,s))
{
i=-1;
while(s[++i])X[i]=ctoi(s[i]);
l=i--;
j=0;
while(j<i)
{
t=X[j];
X[j++]=X[i];
X[i--]=t;
}
j=0;
while(l>0)
{
for(i=l-1; i>0; --i)
{
X[i-1]+=(X[i]%N)*M;
X[i]/=N;
}
Y[j++]=X[0]%N;
X[0]/=N;
if(!X[l-1])--l;
}
Y[j]=0;
while(j&&Y[j]==0)--j;
if(j==0&&Y[0]==0)putchar(‘0‘);
else
{
++j;
while(j--)putchar(itoc(Y[j]));
}
puts("");
}
}
标签:
原文地址:http://www.cnblogs.com/windcarp/p/4743188.html