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;
}
上一篇: 重建二叉树
下一篇: JS解决URL传递参数中文乱码问题
推荐阅读
-
丑数(Ugly Numbers, UVa 136)
-
UVA 136 - Ugly Numbers(优先队列 + 集合)
-
(UVa 136) Ugly Numbers(丑数的生成+整数分解定理+优先队列)
-
Uva 11582 Colossal Fibonacci Numbers! (循环节、幂取模、打表)
-
丑数(Ugly Numbers, UVa 136)
-
习题3-12 浮点数(Floating-Point Numbers,UVa11809)
-
UVA - 11582 Colossal Fibonacci Numbers!
-
Floating-Point Numbers UVA - 11809
-
Specialized Four-Digit Numbers POJ 2196 ZOJ 2405 UVA 3199