@xunuo
2016-11-28T23:02:06.000000Z
字数 2605
阅读 879
Time Limit:500MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
链接:http://vjudge.net/contest/142050#problem/K
高精度
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.
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.
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.
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
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 */
{
...
}
题意:
- 就是一个裸的浮点高精度乘方
- 乘方与乘法相似!!就只是多了一重循环而已
- 要注意前导零、后导零、小数点这些问题
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int a[10000],b[10000],mul[10000];
char s[10000];
int main()
{
int x,n,num=0;
while(scanf("%s%d",s,&x)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(mul,0,sizeof(mul));
int l1=strlen(s);
int l2=1;
int j=0;
b[0]=1;
for(int i=0;i<l1;i++)
{
if(s[i]=='.')
n=(l1-1-i)*x;///计算小数最终的位数;
else
{
a[l1-2-j]=s[i]-'0';///去小数点;
j++;
}
}
l1--;
///乘方的计算!!!
for(int i=0;i<x;i++)
{
for(int j=0;j<l1;j++)
for(int k=0;k<l2;k++)
{
mul[k+j]+=a[j]*b[k];
if(mul[k+j]>=10)
{
mul[k+j+1]+=(mul[k+j])/10;
mul[k+j]=mul[k+j]%10;
}
}
int l3=l1+l2;
memset(b,0,sizeof(b));
for(int j=0;j<l3;j++)
{
b[j]=mul[j];
}
l2=l3;
memset(mul,0,sizeof(mul));
}
int l=l1*x;
int sum=0;
for(int i=0;i<l1;i++)
sum+=a[i];
if(sum==0)
printf("0");///特判0;
else
{
///去前导零;
for(int i=l-1;i>0;i--)
{
if(b[i]==0)
l--;
else
break;
}
///当结果只存在小数部分的时候!!!
if(l<=n)
{
printf(".");
for(int i=0;i<n-l;i++)
printf("0");
int i;
///小数中最后的0;
for(i=0;i<l-1;)
{
if(b[i]==0)
i++;
else
break;
}
for(int j=l-1;j>=i;j--)
printf("%d",b[j]);
}
///有整数部位时!!
else
{
///小数中最后的0;
int i;
for(i=0;i<l;)
{
if(b[i]==0&&n>i)
i++;
else
break;
}
///输出!!
for(int j=l-1;j>=i;j--)
{
if(j+1==n)
printf(".");
printf("%d",b[j]);
}
}
}
printf("\n");
}
return 0;
}