杭电ACM step 2.3.2

本文详细介绍了如何使用高精度算法处理非常大数值的乘法运算,特别是涉及小数点位置处理和后置零的问题。通过具体示例和C++代码,展示了如何读取、存储和计算大实数的幂,以及如何正确输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 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 Rn 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

思路

找小数点的位置,记录。将字符串转换成数字,然后高精度算法,模拟乘法,算出来数。输出,注意小数点的位置和后置0的问题

代码+注释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

//根据乘法的原理,先相乘,在点小数点 
//定义一个高精度实数 
struct BigReal
{
	int len;	//长度 
	int num[10000];
	int point;	//小数点位置 
	//构造函数 
	BigReal()
	{
		len=1;
		point=0;
		memset(num,0,sizeof(num));
	}
};

//读大实数 
bool Read(BigReal &a)
{
	string s;	//读入一个字符串 
	int t,i;
	
	if(cin>>s)
	{
		a.len=s.size();	//求长度
		a.point=0;		//初始为0
		t=0;
		//从后面往前的原因,容易找小数点的位置
		//从前面相乘是不确定的 
		for(i=s.size()-1;i>=0;i--)
		{
			if(s[i]=='.')
			{
				a.len--;
				a.point=t;
				continue;
			}
			//不相等 
			//将字符串读入到了a.num的数组中,不包括小数点 
			a.num[t]=s[i]-'0';
			t++;
		 } 
		 
		 return true;
	}
	else
		return false;
}

//输出 
void Show(BigReal& a)
{ 
	int i,pos;
	//小于1,pos就是小数点后面0的个数
	//由于逆序输出,pos是后导0,最后面0的个数 
	//大于1,pos=0 
	for(i=0;i<a.point&&a.num[i]==0;i++);
	pos=i;
	
	//小数点位数=长度
	//即小于1 
	if(a.point==a.len)
	{
		//如果是0.00000 
		if(pos==a.point)
		{
			cout<<0<<endl;
			return;
		}
		//只有小数 
		else
			cout<<'.';
	}
	//从最后一个长度开始 
	for(i=a.len-1;i>=0;i--)
	{
		//如果是小于1的数,相当于直接输出了数 
		//小于1,不会遇到i=a.point的情况,因为a.point==a.len 
		cout<<a.num[i];
		//小数时不输入后导0 
		//输入的都是四位有效数字,所以一定有后导0(在0.0000的情况下)
		//是整数,输入到后导0的位置,跳出不输入 
		if(i==pos)
			break;
		//到小数点的位置,输入小数点 
		if(i==a.point)
			cout<<'.';
	}
	cout<<endl;
 } 

 //相乘
 BigReal Mul(const BigReal& a,const BigReal& b)
 {
 	int i,j,len=0;
 	BigReal c;
 	
 	for(i=0;i<a.len;i++)
 	{
 		for(j=0;j<b.len;j++)
 		{
 			c.num[i+j]=c.num[i+j]+a.num[i]*b.num[j];
 			//大于10,做进位处理 
 			if(c.num[i+j]>=10)
 			{
 				c.num[i+j+1]=c.num[i+j+1]+c.num[i+j]/10;
 				c.num[i+j]=c.num[i+j]%10;
			}
		}
	}
	c.point=a.point+b.point;
	len=a.len+b.len;
	//处理前置0 ,注意小数点的位数
	//有可能0是小数点后面的0 
	while(c.num[len-1]==0&&len>1&&len>c.point)
		len--;
	//不等于0,位数加一 
	if(c.num[len])
		len++;
	c.len=len;
	return c;
 } 

 int main()
 {
 	BigReal a;
 	int b;
 	
 	while(Read(a)&&scanf("%d",&b)!=EOF)
 	{
 		BigReal ans;
 		ans.num[0]=1;
 		//相乘第一次就变成了a,相当于a做a的乘方 
 		while(b--)
 			ans=Mul(ans,a);
 			
 		//输出 
 		Show(ans);
	}
	return 0;
} 
  

注:
代码参考
https://www.cnblogs.com/mfrbuaa/p/5202670.html的源代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值