UVA424 (高精度)

题目:

One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.

``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output

370370367037037036703703703670

题目大意:每行为一个数字,对输入的所有数字相加。

方法:从后位算起,将递进的数值累加到前一位然后再实现相加。

由于最后可能会出现累加的值没有递进的情况,所以在输入0时将最后累加的数组加一次0实现递进,然后再break。

代码:

#include<stdio.h>
#include<string.h>
int main () {
	char a[150];
	int b[150]={0};
	int i,j,k,temp;
	while(scanf("%s",a)) {	
		if(a[0]=='0') {
			for (i=149;b[i]==0;i-- );
			for(;i>=0;i--) {
				temp=b[i];
				b[i]=temp%10;
				b[i+1]+=temp/10;
			}
			break;
		}
		for(i=strlen(a)-1,j=0;i>=0;i--,j++) {
			temp=b[j]+(a[i]-'0');
			b[j]=temp%10;
			b[j+1]+=temp/10;
		}
	}
	for(i=149;b[i]==0;i--);
	for(;i>=0;i--) {
		printf("%d",b[i]);
	}
	printf("\n");
	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、付费专栏及课程。

余额充值