uva748 - Exponentiation

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999) and n is an integer such that .

Input 

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output 

The output will consist of one line for each line of input giving the exact value of Rn. Leading zeros and insignificant trailing zeros should be suppressed in the output.

Sample Input 

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Output 

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

计算实数的n次幂,先记录下小数点的位置如第一个是倒数第三个,那么12次幂以后就是倒数36个,然后当成整数乘法做。

需要处理小数点之前的前缀0和小数点之后的后缀0;

题目说了是实数,但貌似没说一定有小数点(ˇˍˇ) 想~,我按照一定有小数点做的ac了╮(╯▽╰)╭。

#include <stdio.h>
#include <string.h>
#define max 3000
int c[max],postion;
int change(int a[],char s[])
{int l=strlen(s),i,point=0;
 for (i=1;i<max;i++)
 a[i]=0;
 for (i=l-1;i>=0;i--)
  if (s[i]=='.') {point=1;postion=l-i-1;}
 else a[l-i-point]=s[i]-'0';
 return l-1;
};
int sort(int a[],int l1,char s[])
{int len,l2,i,j,b[max];
 l2=change(b,s);
 for (i=1;i<max;i++)
  c[i]=0;;
 for (i=1;i<=l1;i++)
 for (j=1;j<=l2;j++)
 c[i+j-1]=c[i+j-1]+a[i]*b[j];
 for (i=1;i<=l1+l2;i++)
 {c[i+1]=c[i+1]+c[i]/10;
  c[i]=c[i]%10;
 }
 len=l1+l2;
 for (i=1;i<=len;i++)
 {a[i]=c[i];c[i]=0;}
 return len;
};
void main()
{char s[max];
 int i,l1,w,a[max],end;
 while (scanf("%s %d",&s,&w)!=EOF)
 {for (i=1;i<max;i++) c[i]=0;
  l1=change(a,s);
  for (i=1;i<w;i++)
  l1=sort(a,l1,s);
  postion*=w;
  while (l1>postion&&a[l1]==0) --l1;
  end=1;
  while (a[end]==0) ++end;
  for (i=l1;i>=end;i--)
  {if (i==postion) printf(".");
   printf("%d",a[i]);
  }
  printf("\n");
 }
}

你可能感兴趣的:(uva748 - Exponentiation)