这道题很然我有感触,碰到这种题总是手足无措。
思路是借鉴网上的,这里只是我的反思。
1.打表查询
2.精度问题的解决
3.学习对题目进行分析,有的计算不能直接算,但是有效分析之后就可以间结算出。
4.学会有STL简化代码
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <sstream>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#define FIN freopen("input.txt","r",stdin)
using namespace std;
typedef long long ll;
int main() {
double M[20][40];
ll E[20][40];
// 打表
for(int i = 0; i <= 9; ++i) for(int j = 1; j <= 30; ++j) {
double m = 1 - pow(2, -1 - i), e = pow(2, j) - 1;
double t = log10(m) + e * log10(2);
E[i][j] = t, M[i][j] = pow(10, t - E[i][j]);
}
// 输入并输出结果
//FIN;
string in;
while(cin >> in && in != "0e0") {
// 处理输入
for(string::iterator i = in.begin(); i != in.end(); ++i) if(*i == 'e') *i = ' ';
stringstream ss(in);
double A; int B;
ss >> A >> B;
//把不符合科学计数法的数转化为科学计数法
while(A < 1) A *= 10, B -= 1;
// 在打好的表中寻找答案
for(int i = 0; i <= 9; ++i) for(int j = 1; j <= 30; ++j) {
if(B == E[i][j] && (fabs(A - M[i][j]) < 1e-4 || fabs(A / 10 - M[i][j]) < 1e-4)) {
cout << i << ' ' << j << endl;
break;
}
}
}
return 0;
}