uva 748(高精度)

题目:

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 ofRn where R is a real number ( 0.0 <R < 99.999) and n is an integer such that $0 < n \le 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.

outout

The output will consist of one line for each line of input giving the exact value of Rn. Leading zeros and insignificant trailing zeros should be suppressed in the output.

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

题解:高精度求幂,主要是小数点的摆放,和前后清零,读入最好将小数和幂分开读。


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

const int N = 500;
int r[N] , n;
int count(int m) {
	if (n == 0)
		return m;
	int ans[N], temp[N], i ,k, j, temp1;
	for (i = 0; i < m; i++)
		temp[i] = r[i];//保存原来的数组
	temp1 = m;//保存原来的位数
	while (n--) {
		memset(ans, 0, sizeof(ans));
		for (i = temp1 - 1; i >= 0; i--)
			for (j = m - 1, k = temp1 - 1 - i; j >= 0; j--)
				ans[k++] += r[i] * temp[j];
		for (i = 0; i < N; i++) {
			ans[i + 1] += ans[i] / 10;
			ans[i] %= 10;
		}
		for (i = N - 1; i >= 0; i--)
			if (ans[i] != 0)
				break;
		for (k = i, j = 0; k >= 0; k--, j++)
			r[j] = ans[k];
		temp1 = i + 1;
	}
	m = i + 1;
	return m;
}

int main(){
	char str[7];
	int pos, i, m, j, flag;
	while (~scanf("%s", str)) {
		n = m = flag = 0;
		pos = -1;
		memset(r, 0, sizeof(r));
		cin >> n;
		int len = strlen(str);
		for (i = 0; i < len; i++)//清除前面的零
			if (str[i] != '0')
				break;
		if (i == len) {//全部是零
			cout << '0' << endl;
			continue;
		}
		for (j = i; j < len; j++) {
			if (str[j] >= '0' && str[j] <= '9')
				r[m++] = str[j] - '0';//存储不包含小数点的数
			else if (str[j] == '.') {
				pos = m;//记录小数点的位置
				flag = 1;
			}
		}
		if (flag == 1)//如果有小数点
			for (i = m - 1; i >= pos; i--)
				if (r[i] != 0)//清除后面的零
					break;
				else
					m--;
		if (m == 0) {//全部是零
			cout << '0' << endl;
			continue;
		}
		int temp = n;
		n--;
		int a = count(m);
		if (flag == 0) {//无小数点输出
			int k;
			for (k = 0; k < a; k++)
				if (r[k] != 0)
					break;
			for (int q = k; q < a; q++)
				cout << r[q];
			cout << endl;
		}
		else {//需要添加小数点
			int k, t = 1, q;
			pos = (m - pos) * temp;//小数点要移动的次数
			if ((a - pos) >= 0) {//次数大于结果的总位数
				for (q = 0; q < a; q++, t++)
					if (t != (a - pos + 1))
						cout << r[q];
					else {
						cout << '.';
						q--;
					}
				cout << endl;
			}
			else {//需要在结果前补零
				cout << '.';
				int c = pos - a;
				while (c--) 
					cout << '0';
				for (k = 0; k < a; k++)
					cout << r[k];
				cout << endl;
			}
		}
	}

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值