[1001]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 Rnwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

输入

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.

输出

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.

样例输入

95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12

样例输入

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

这是一道大数乘法的变形题。总体思路是分两步走

  1. 计算出最终结果的小数位数。这个可以由 n × R 的 小 数 位 数 n×R的小数位数 n×R得到,非常容易
  2. 将R去掉小数点得到一个整数,计算这个整数的n次方,Rn=R×R×R×R…×R(大数乘法),这里选择模拟乘法累加

代码

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

#define MAX 160
#define NUM 5
int result[MAX], jieguo[MAX], number[NUM];

int main() {
	string r;
	int n;
	while (cin >> r >> n) {
		//初始化 
		for (int i = 0; i < MAX; i++) {
			result[i] = 0;
			jieguo[i] = 0;
		}
		for (int i = 0; i < NUM; i++) {
			number[i] = 0;
		}
		//找出小数点的位置 
		int pos=NUM;
		for (int i = NUM, j = 0; i >= 0; i--) {
			if (r[i] != '.') {
				jieguo[j] = r[i] - '0';
				number[j] = r[i] - '0';
				j++;
			}
			else {
				pos = i;
			}
		}
		//最终结果的小数位数 
		int posnum = (NUM - pos) * n;
		while (n >= 2) {
			for (int j = 0; j < MAX; j++) result[j] = 0;
			for (int i = 0; i < NUM; i++) {
				for (int j = 0; j < MAX; j++) {
					if (number[i] == 0) break;
					int res = jieguo[j] * number[i];
					result[i + j] += res;
					int k = i + j;
					for (; result[k] > 9; k++) {
						int temp = result[k];
						result[k + 1] += temp / 10;
						result[k] = temp % 10;
					}
				}
			}
			for (int i = 0; i < MAX; i++) jieguo[i] = result[i];
			--n;
		}

		int start=-1, end=-1;
		for (int i = 0; i < posnum; i++) {
			if (jieguo[i] != 0) {
				start = i;
				break;
			}
		}
		for (int i = MAX-1; i >= posnum; i--) {
			if (jieguo[i] != 0) {
				end = i;
				break;
			}
		}

		if (end != -1) {
			for (int i = end; i >= posnum; i--) {
				cout << jieguo[i];
			}
		}
		if (start != -1) {
			cout << ".";
			for (int i = posnum - 1; i >= start; i--) {
				cout << jieguo[i];
			}
		}
		cout << endl;
	}
}

注意:

  • 如果计算出来是个整数那么结果中是没有小数点的,前几次没有通过就是因为忽略了这一点
  • 倒序存储数字:例如95123×14
    在这里插入图片描述
    大数乘法代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char a1[10001], b1[10001];
int a[10001], b[10001], i, x, len, j, c[10001];
int main()
{
	cin >> a1 >> b1;//不解释,不懂看前面
	int lena = strlen(a1);//每个部分都很清楚
	int lenb = strlen(b1);//这只是方便你们复制
	for (i = 1; i <= lena; i++) a[i] = a1[lena - i] - '0';  //倒叙存储
	for (i = 1; i <= lenb; i++) b[i] = b1[lenb - i] - '0';

	for (i = 1; i <= lenb; i++)
		for (j = 1; j <= lena; j++)
			c[i + j - 1] += a[j] * b[i];   //i是错位

	for (i = 1; i < lena + lenb; i++)
		if (c[i] > 9)
		{
			c[i + 1] += c[i] / 10;
			c[i] %= 10;
		}

	len = lena + lenb;
	while (c[len] == 0 && len > 1)len--;
	for (i = len; i >= 1; i--)cout << c[i];
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值