[关闭]
@xunuo 2016-11-28T15:02:06.000000Z 字数 2605 阅读 822

Exponentiation

Time Limit:500MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

链接:http://vjudge.net/contest/142050#problem/K

高精度


Description

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 R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

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 R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

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

Hint

If you don't know how to determine wheather encounted the end of input: 
s is a string and n is an integer 
C++

while(cin>>s>>n)

{

...

}

c

while(scanf("%s%d",s,&n)==2) //to  see if the scanf read in as many items as you want

/*while(scanf(%s%d",s,&n)!=EOF) //this also work    */

{

...

}

题意:

  • 就是一个裸的浮点高精度乘方
  • 乘方与乘法相似!!就只是多了一重循环而已
  • 要注意前导零、后导零、小数点这些问题

完整代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. using namespace std;
  6. int a[10000],b[10000],mul[10000];
  7. char s[10000];
  8. int main()
  9. {
  10. int x,n,num=0;
  11. while(scanf("%s%d",s,&x)!=EOF)
  12. {
  13. memset(a,0,sizeof(a));
  14. memset(b,0,sizeof(b));
  15. memset(mul,0,sizeof(mul));
  16. int l1=strlen(s);
  17. int l2=1;
  18. int j=0;
  19. b[0]=1;
  20. for(int i=0;i<l1;i++)
  21. {
  22. if(s[i]=='.')
  23. n=(l1-1-i)*x;///计算小数最终的位数;
  24. else
  25. {
  26. a[l1-2-j]=s[i]-'0';///去小数点;
  27. j++;
  28. }
  29. }
  30. l1--;
  31. ///乘方的计算!!!
  32. for(int i=0;i<x;i++)
  33. {
  34. for(int j=0;j<l1;j++)
  35. for(int k=0;k<l2;k++)
  36. {
  37. mul[k+j]+=a[j]*b[k];
  38. if(mul[k+j]>=10)
  39. {
  40. mul[k+j+1]+=(mul[k+j])/10;
  41. mul[k+j]=mul[k+j]%10;
  42. }
  43. }
  44. int l3=l1+l2;
  45. memset(b,0,sizeof(b));
  46. for(int j=0;j<l3;j++)
  47. {
  48. b[j]=mul[j];
  49. }
  50. l2=l3;
  51. memset(mul,0,sizeof(mul));
  52. }
  53. int l=l1*x;
  54. int sum=0;
  55. for(int i=0;i<l1;i++)
  56. sum+=a[i];
  57. if(sum==0)
  58. printf("0");///特判0;
  59. else
  60. {
  61. ///去前导零;
  62. for(int i=l-1;i>0;i--)
  63. {
  64. if(b[i]==0)
  65. l--;
  66. else
  67. break;
  68. }
  69. ///当结果只存在小数部分的时候!!!
  70. if(l<=n)
  71. {
  72. printf(".");
  73. for(int i=0;i<n-l;i++)
  74. printf("0");
  75. int i;
  76. ///小数中最后的0;
  77. for(i=0;i<l-1;)
  78. {
  79. if(b[i]==0)
  80. i++;
  81. else
  82. break;
  83. }
  84. for(int j=l-1;j>=i;j--)
  85. printf("%d",b[j]);
  86. }
  87. ///有整数部位时!!
  88. else
  89. {
  90. ///小数中最后的0;
  91. int i;
  92. for(i=0;i<l;)
  93. {
  94. if(b[i]==0&&n>i)
  95. i++;
  96. else
  97. break;
  98. }
  99. ///输出!!
  100. for(int j=l-1;j>=i;j--)
  101. {
  102. if(j+1==n)
  103. printf(".");
  104. printf("%d",b[j]);
  105. }
  106. }
  107. }
  108. printf("\n");
  109. }
  110. return 0;
  111. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注