Exponentiation

Exponentiation

Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9689    Accepted Submission(s): 2876


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

Source
 

Recommend
PrincetonBoy




解题思路

利用大数组进行储存每一位相乘后得到的数字,然后不断地用底数乘每一位数字,/10进位,%10得到进位后这个位置的数字。

难点

1、多大的数组好

99.999,可以类似看成10的五次方,10的25*5=125次方,数组开个130就差不多了。

2、91.235我是把它变成一个整数然后记录究竟有多少位小数,先用这个整数完成全部操作最后再在适当的位置输出小数点。

我们得到的数字是double类型的,也就是91.235这样子的,但是我们如果用强制转换来得到一个int很有可能会出现精度损失这样子的问题。所以我在这里推荐的是利用string,


也很好操作

for(int i=0;i<m.size();i++)
		{
			if(m[i]!='.')
			{
				m2=m2*10+(m[i]-'0');
			}
		}

小细节


记得把string末尾的0
erase掉


3、如何补0操作


判断最终操作完成后究竟有多少位,如果小数位数比实际得到的位数还要大那么我们就补0;



4、坑人的地方

他说了类似99.999这样子的操作,让我误以为全部都是这样子的,包括10.000,但是实际上并不是!他的输入很有可能是10,就这样子一个10,这样子我一直AC不了,所以针对我的程序我需要判断有没有小数点,如果真的像上述这样子,不是输入10.000而是10,那么我需要手动加一个小数点


代码展示

#include<iostream>
#include<string>
using namespace std;
int a[130];
int main()
{
	string m;
	while (cin >> m)
	{
		if (m.find(".") == string::npos)
		{
			m = m + '.';
		}
		for (int i = 0; i<130; i++)
			a[i] = -1;
		int n = 0;
		scanf("%d", &n);
		while (m[m.size() - 1] == '0')
		{
			m.erase(m.size() - 1, 1);
		}
		int digit = 0;
		for (int i = m.size() - 1; m[i] != '.'; i--)
			digit++;
		int m2 = 0;
		for (int i = 0; i<m.size(); i++)
		{
			if (m[i] != '.')
			{
				m2 = m2 * 10 + (m[i] - '0');
			}
		}
		digit = digit*n;
		for (int i = 0, tmp = m2;; i++)
		{
			a[i] = tmp % 10;
			if (tmp / 10 == 0)
				break;
			else
				tmp = tmp / 10;
		}
		n--;
		while (n--)
		{
			for (int k = 0; a[k] != -1; k++)
			{
				a[k] = a[k] * m2;
			}
			for (int k = 0; a[k] != -1; k++)
			{
				if (a[k] >= 10)
				{
					if (a[k + 1] == -1)
						a[k + 1] += a[k] / 10 + 1;
					else
						a[k + 1] += a[k] / 10;
					a[k] = a[k] % 10;
				}
			}
		}
		int location = 0;
		for (;; location++)
		{
			if (a[location] == -1)
			{
				break;
			}
		}
		location--;
		if (digit>(location + 1))
		{
			printf(".");
			while (digit>(location + 1))
			{
				cout << "0";
				digit--;
			}
			for (; location >= 0; location--)
			{
				printf("%d", a[location]);
			}
			printf("\n");
		}
		else
		{
			for (; location >= 0; location--)
			{
				if (location == digit - 1)
					printf(".");
				printf("%d", a[location]);
			}
			printf("\n");
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值