<分区>
我有以下功能:
template <class T>
T c_base (T num,T second, T first = 10)
{
T res = 0;
T secnum;
T bitseed[90];
int i = 1,k,jump,anex,len;
if(second==first)
{
res = num;
return (res);
}
if(first==10&&second!=10)
{
anex = num;
while(num>0)
{
jump = num/second;
bitseed[i] = num%second;
num/=second;
i++;
}
if(anex>0)
{
for(k=i;k>=1;k--)
{
if(k==i&&jump==0) {res = bitseed[k-1]; k--; continue;}
if(k==i&&jump!=0) {res = jump; continue;}
res = res*10+bitseed[k];
}
}
return (res);
}
if(second==10)
{
anex = num;
len = 1;
while(anex>=10)
{
len *= 10;
anex/=10;
i++;
}
anex = num;
if(anex>0)
{
for(k=i;k>=1;k--)
{
res = res*first+anex/len;
anex%=len;
len/=10;
}
}
return (res);
}
if(second!=10&&first!=10)
{
secnum = c_base <T> (num,10,first);
res = c_base <T> (secnum,second,10);
return (res);
}
}
我想知道它的效率如何(从速度和内存消耗的角度来看)以及如何/是否可以改进它。 (从算法角度)
附言。函数说明:c_base("number","to-base","from-base"->optional);