【vijos P1010】清帝之惑之乾隆 c++题解

来源:互联网 发布:征途服务端linux版 编辑:程序博客网 时间:2024/04/28 13:39

题目描述

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 <= 9999.9) and n is an integer such that 0 < n <= 250.

输入格式


The input will consist of a set (less than 11) 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 to 10.

输出格式


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.

#include <stdio.h>#include <string>#include <iostream>#define MaxM 6100using namespace std;int c[MaxM];class Num{public :int len,point;int a[MaxM];void init(){len=0;point=0;for (int i=0;i<MaxM;i++) a[i]=0;}void rev(){for (int i=1;i<=len/2;i++){int tmp;tmp=a[i];a[i]=a[len-i+1];a[len-i+1]=tmp;}}void outfile(){int i=1,j=len;while (a[i]==0&&i<=len) i++;while (a[j]==0&&j>=1&&j>point) j--;for (j;j>=i;j--){if (j==point) cout<<'.';cout<<a[j];}cout<<endl;}Num &operator*(Num y){int i;for (int i=1;i<=len;i++) {c[i]=a[i];a[i]=0;}for (int i=1;i<=len;i++)for (int j=1;j<=y.len;j++){a[i+j-1]+=c[i]*y.a[j];a[i+j]+=a[i+j-1]/10;a[i+j-1]%=10;}len=len*y.len;point+=y.point;while (a[len]==0&&len>point+1) len--;return *this; }};Num P,Ans;void work(){string s;char ch;int N,count=0;while (ch!=EOF){cin>>s>>N;count++;if (N<0) return ;P.init(); Ans.init();P.len=1;P.a[1]=s[0]-'0';for (int i=1;i<=5;i++){if (s[i]=='.') P.point=5-P.len;else P.a[++P.len]=s[i]-'0';}P.rev();Ans.len=Ans.a[1]=1; Ans.point=0;for (int i=1;i<=N;i++) Ans=Ans*P;Ans.outfile();N=-1;}}int main(){freopen("p1010.in","r",stdin);freopen("p1010.out","w",stdout);work();return 0;} 


0 0
原创粉丝点击