HDU1063-Exponentiation(大数的计算)

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

分析:

题意:
一个实数p和一个整数n,求qn得结果,省去多余得0!

解析:
一个比较好的大数计算题,大数计算就是那样,也没有什么好说的!

代码:

#include<iostream>
#include<cstdio>
#include<algorithm> 
#include<cmath>

using namespace std;

int result[200];

void Print(int l,int r)
{
	for(int i=r;i>=l;i--)
	{
		printf("%d",result[i]);
		result[i]=0;
	}
}

int main()
{
	string str;
	int n,len,l,v;
	while(cin>>str>>n)
	{
		v=l=len=0;
		for(int i=1;i<=str.length();i++)
		{
			if(str[i-1]!='.')
				v=v*10+str[i-1]-'0';
			else
				len=str.length()-i;
		}
		if(!v)
			printf("0\n");
		else
		{
			result[0]=result[1]=1;
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=result[0];j++)
				{
					result[j]=v*result[j];
				}
				int len1=1;
				while(result[len1]||len1<=result[0])
				{
					result[len1+1]+=result[len1]/10;
					result[len1]=result[len1]%10;
					len1++;
				}
				result[0]=len1-1;
			}
			while(!result[++l]);
			len=len*n;
			if(len>=result[0])
			{
				printf(".");
				Print(l,len);
			}
			else
			{
				if(!len)
					Print(1,result[0]);
				else
				{
					if(l>len)
						Print(len+1,result[0]);
					else
					{
						Print(len+1,result[0]);
						printf(".");
						Print(l,len);
					}
				}
			}
			printf("\n");
		}
	}
	return 0;
 }

猜你喜欢

转载自blog.csdn.net/weixin_43357583/article/details/106009479