UVA - 11809 Floating-Point Numbers

//

第三章的最后一道题, 当时没敢看, 也是醉了。 后来ACM群里讨论了, 有点印象, 现在仔细看也不是很难。

//

1、那个样例的第二个A(算法入门)是0.5*1+0.5/2+0.5/2/2 +0.5/...2(除八个二), 这是计算机的储存方式。 仅仅知道这里而已。

2.INF要取到10的-4-6都行, 但是10的-10不行。

3.两边对数, 打表, 直观。

<pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <string.h>
#include<math.h>
#include<algorithm>
const int maxn = 30 + 5;
const double INF = 1e-6;
using namespace std;

const int E[maxn] = {0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383,
32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215, 33554431,
67108863, 134217727, 268435455, 536870911, 1073741823};

const double M[maxn] = {0.5, 0.75, 0.875, 0.9375,0.96875, 0.984375, 0.9921875, 0.99609375, 0.998046875,
0.9990234375, 0.99951171875};

char str[maxn];

int main()
{
    while(scanf("%s", str) && strcmp(str, "0e0") != 0)
    {
        *(strchr(str, 'e')) = ' ';

        int EE, B;
        double MM, A, BB;

        sscanf(str, "%lf %d", &A, &B);

        BB = log10(A) + B;

        for(int i = 0; i <= 9; i++)
        {
            for(int j = 1; j <= 30; j++)
            {
                if(fabs( (log10(M[i]) + E[j] * log10(2)) - BB) < INF)
                {
                    printf("%d %d\n", i, j);
                    break;
                }
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值