| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
额,思路蛮清晰的,就是感觉有点复杂,附上 AC代码In Reply To:搞出来了!绝对是自己写的!附上讲解! Posted by:chenxuan123456789 at 2012-08-03 18:41:54
把大数据结构体封装一下,然后重载操作符号,这样书写代码就清爽多了. 时间是0MS
#include <stdio.h>
#include <string.h>
#define MAXLEN 180
typedef struct st_bignumber
{
int nLen ;
int nData[MAXLEN] ;
st_bignumber()
{
reset();
} ;
st_bignumber(const struct st_bignumber &other)
{
nLen = other.nLen ;
memcpy(nData,other.nData,sizeof(nData[0])* nLen) ;
} ;
void reset()
{
nLen = 0 ;
memset(nData,0,sizeof(nData)) ;
}
st_bignumber operator = ( const struct st_bignumber & other)
{
nLen = other.nLen ;
memset(nData,0,sizeof(nData)) ;
memcpy(nData,other.nData,sizeof(nData[0])* nLen) ;
return * this ;
}
st_bignumber operator * ( const struct st_bignumber & other)
{
struct st_bignumber res ;
int i ,j ;
for ( i = 0 ;i < nLen ;i ++ )
{
for (j = 0 ;j < other.nLen; j ++ )
{
res.nData[i+j] += this->nData[i] * other.nData[j] ;
}
}
for( i = 0 ;i < nLen + other.nLen ;i ++)
{
if( res.nData[i] >= 10)
{
res.nData[i+1] += ( res.nData[i] / 10) ;
res.nData[i] %= 10 ;
}
if( res.nData[i] != 0)
res.nLen = i + 1 ;
}
return res ;
}
}bignumber;
int main( void )
{
char s[10];
int n , i ;
int nRealCount ;
bignumber number,result, tmp;
while(scanf("%s%d",s,&n)==2)
{
nRealCount = 0 ;
number.reset() ;
for( i = strlen(s) -1 ;i >= 0 ;i -- )
{
if( s[i] =='0')
s[i] = '\0' ;
else
break ;
}
if( i == -1 )
{
printf("0\n") ;
continue;
}
for (i = strlen(s) - 1;i >= 0 ; i --)
{
if( s[i] == '.')
{
nRealCount = strlen(s) - i - 1 ;
continue ;
}
number.nData[number.nLen++] = s[i] - '0' ;
}
tmp = number ;
result.nData[0] = 1 ;
result.nLen = 1 ;
nRealCount *= n ;
while( n > 1)
{
if( n & 1)
{
result = result * tmp ;
n -- ;
}
tmp = tmp * tmp ;
n >>= 1 ;
}
result = result * tmp ;
if( nRealCount >= result.nLen)
{
printf(".") ;
for( i = 0 ; i < nRealCount - result.nLen ; i ++ )
printf("0") ;
for( i = result.nLen - 1 ; i >= 0 ; i --)
{
printf("%d",result.nData[i]) ;
}
}else
{
for( i = result.nLen - 1; i >= 0 ; i --)
{
printf("%d",result.nData[i]) ;
if(nRealCount != 0 && i == nRealCount)
printf(".");
}
}
printf("\n");
}
return 0 ;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator