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

题目描述

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 6100

using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值