Floating-Point Numbers UVA - 11809

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40379678/article/details/82833496

觉得有点难、、、

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

猜你喜欢

转载自blog.csdn.net/qq_40379678/article/details/82833496