【习题 3-12 UVA - 11809】Floating-Point Numbers
【链接】 我是链接,点我呀:)
【题意】
【题解】
$A*10^B = temp[M]*2^{2^E-1}$ 两边取一下对数 得到 $lg_A+B = lg_{temp[M]} + (2^E-1)*lg_2$ 这样就不至于算不出来啦。 打个表就好 防止爆精度。 加个long double.【代码】
#include <bits/stdc++.h>
using namespace std;
string s;
long double two[20];
long double temp[20];
long long temp2[70];
long double ans[100][100];
int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
two[1] = 0.5;
for (int i = 2;i <= 12;i++){
two[i] = two[i-1]*0.5;
}
temp[0] = two[1];
for (int i = 1;i <= 9;i++) temp[i] = temp[i-1] + two[i+1];
temp2[0] = 1;
for (int i = 1;i <= 30;i++) temp2[i] = temp2[i-1] * 2;
for (int i = 0;i <= 9;i++){
double M = temp[i];
for (int j = 1;j <= 30;j++){
ans[i][j] = log10(M) + (temp2[j]-1)*log10(2);
}
}
while (cin >> s){
int len = s.size();
for (int i = 0;i < len;i++) if (s[i]=='e') s[i] = ' ';
long double A;
long long B;
stringstream ss(s);
ss >> A >> B;
if (A==0 && B==0) break;
double temp = log10(A)+B;
bool fi = false;
for (int i = 0;!fi && i <= 9;i++)
for (int j = 1;!fi && j <= 30;j++)
if (fabs(ans[i][j]-temp)<1e-6){
printf("%d %d\n",i,j);
fi = true;
}
}
return 0;
}