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


### 解决方案概述 对于UVa 11809 浮点数问题,核心挑战在于理解和处理浮点数表示及其精度限制。该问题涉及如何解析给定的二进制字符串来确定其代表的最大十进制数值[^3]。 ### 数据结构与算法设计 #### 输入分析 输入由多组测试案例组成,每组包含两个整数E和M,分别指示阶码长度和尾数长度。目标是从这些参数推导出可表示的最大正实数,并将其转换为科学计数法形式输出。 #### 关键概念解释 - **最大浮点数**:基于指定的E和M值构建最大的浮点数意味着设置所有的位都为最高有效状态——即全设为`1`。 - **指数部分**:考虑到偏置(bias),实际使用的指数应等于给出的E减去一个固定的偏移量;这里假设采用IEEE标准,则bias=(2^(E−1))−1。 - **小数部分**:因为隐含前导'1.'的存在,在计算最终结果时需额外加上这一项。 #### 实现细节 为了实现上述逻辑,可以按照如下方式编写程序: ```cpp #include <iostream> #include <cmath> using namespace std; int main() { int E, M; while (cin >> E >> M && !(E == 0 && M == 0)) { // 结束条件 double max_fraction = pow(2.0, M) - 1; // 尾数全部为1的情况下的分数部分 // 计算指数的实际大小(考虑偏置) int bias = pow(2, E - 1) - 1; int exp_value = (pow(2, E) - 1) - bias; // 构造最大浮点数 double result = (max_fraction / pow(2.0, M)) * pow(2.0, exp_value); cout << "Case " << ++case_num << ": "; printf("%.0f\n", result); // 输出不带小数点的形式 } } ``` 此代码片段展示了如何读取输入、执行必要的数学运算以及格式化输出以满足题目要求。特别需要注意的是,当涉及到浮点数操作时,应当谨慎对待可能出现的舍入误差等问题[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值