欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Floating-Point Numbers UVA - 11809

程序员文章站 2022-04-02 21:27:25
...

觉得有点难、、、

//(1 - 2 ^ (-m - 1)) * 2 ^ (2 ^ e - 1) = A * 10 ^ B
//x = (1 - 2 ^ (-m - 1))
//y = (2 ^ e - 1)
//x * 2 ^ y = A * 10 ^ B
//logx + y * log2 = logA + B

然后打表就行了。

#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 410
#define M 2000100
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const double PI = acos(-1);
int Case = 1;
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define tl tree[o].l
#define tr tree[o].r
#define tw tree[o].w
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

//(1 - 2 ^ (-m - 1)) * 2 ^ (2 ^ e - 1) = A * 10 ^ B
//x = (1 - 2 ^ (-m - 1))
//y = (2 ^ e - 1)
//x * 2 ^ y = A * 10 ^ B
//logx + y * log2 = logA + B

double A[15][40];
ll B[15][40];
string s;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    rep(m, 0, 9) rep(e, 1, 30) {
        double x = 1.0 - pow(2.0, -1 - m);
        double y = pow(2.0, e) - 1.0;
        double num = log10(x) + y * log10(2);
        B[m][e] = num;
        A[m][e] = pow(10.0, num - B[m][e]);
//cout << A[m][e] << endl;
    }

    while(cin >> s && s!= "0e0") {
        for(string::iterator it = s.begin(); it != s.end(); it++)
        if(*it == 'e') {
            *it = ' ';
            break;
        }
        istringstream ss(s);
        double a;
        ll b;
        ss >> a >> b;
//cout << a << ' ' << b << endl;
        while(a < 1.0) a *= 10.0, b--;
//cout << a << ' ' << b << endl;
        rep(m, 0, 9) rep(e, 1, 30) {
            if(b == B[m][e] && (fabs(a - A[m][e]) < eps || fabs(a / 10 - A[m][e]) < eps)) {
                printf("%d %d\n", m, e);
                break;
            }
        }

    }

    return 0;
}

 

相关标签: 数学