传说中最恶心的高精度。。学长说了,这题要是自己出了,,那么你高精度也就学精了。。

我至今不敢去碰。。就是因为太恶心了。下面贴个比较简单的代码。。

#include<stdio.h> 
#include<string.h>
int main() 

 int a[1000], 
  b[1000], 
  c[1000]; 
 char s[10]; 
 int point; 
 int n,m; 
 int x; 
 int i, 
  j; 
 int count; 
 int count2,count3;   
 while(scanf("%s %d",&s,&n)!=EOF) 
 {  
  x=strlen(s)-1; //有可能小于6 
  for(i=0,j=x-1;i<=x;i++) 
  { 
   if(s[i]<48) 
    point=x-i; 
   else 
   { 
    a[j]=b[j]=s[i]-48; 
    j--; 
   }   
  } 
  
  count=x-1; 
  
  m=n-1; 
  while(m--) 
  { 
   memset(c,0,1000);  //记得将贮存数组清零 
   for(i=0;i<=count;i++) 
    for(j=0;j<=x-1;j++) 
     c[i+j]+=a[i]*b[j]; 
    
    count+=x-1; 
    for(j=0;j<count;j++) 
    { 
     c[j+1]+=c[j]/10; 
     c[j]=c[j]%10; 
    } 
    if(c[count]/10!=0)  //进行乘法运算的核心步骤 
    { 
     c[count+1]=c[count]/10; 
     c[count]=c[count]%10; 
     count++; 
    } 
    for(j=0;j<=count;j++) 
     a[j]=c[j]; 
    
  } 
  
  for(j=0,count2=0;j<=point*n-1;j++)   //去掉后面多余的0 
   if(a[j]==0) 
    count2++; 
   else  
    break; 
   for(j=count,count3=0;j>=point*n;j--)  //去掉前面多余的0 
    if(a[j]==0) 
     count3++; 
    else  
     break; 
    
    for(j=count-count3;j>=point*n;j--)  
     printf("%d",a[j]); 
    if(count2!=n*point)  //去掉整数后面的小数点 
    { 
     printf("."); 
     for(j=point*n-1;j>=count2;j--) 
      printf("%d",a[j]); 
    } 
    printf("\n"); 
    
    
 }   
    return 0;
}

posted on 2011-08-11 15:09  →木头←  阅读(242)  评论(0编辑  收藏  举报