UVa 11809 - Floating-Point Numbers 题解

这题目好难理解,看了一下午效率好低,看着题解做出来的...不过也算把刘汝佳老师第三章的题全做完了...

 

题意:将一浮点数转换成标准储存需要多少位尾数和指数

思路:因输入为响应最大值,为对应关系,故可打表,枚举m位与e位,存储相应的科学记数法表示,输入时再查表。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <cmath>
  6 #include <algorithm>
  7 #include <set>
  8 #include <map>
  9 #include <string>
 10 #include <queue>
 11 #include <stack>
 12 #include <vector>
 13 #include <fstream>
 14 #include <cctype>
 15 #include <ctime>
 16 #include <iomanip>
 17 #include <sstream>
 18 
 19 #define fi for (int i = 1; i <= n; ++i)
 20 #define fj for (int j = 1; j <= m; ++j)
 21 #define INF 0x3fffffff
 22 #define pau system("pause")
 23 #define printclock printf("Time used = %.2fs\n", (double) clock() / CLOCKS_PER_SEC);
 24 #define eps 1e-5
 25 #define cin std::cin
 26 #define cout std::cout
 27 #define endl std::endl
 28 #define pair std::pair
 29 
 30 const double pi = acos(-1.0);
 31 const int max_n = 1e5 + 5;
 32 typedef long long ll;
 33 typedef pair<int, int> pii;
 34 
 35 template<class Q> void getint(Q &res, char ch = getchar()) {
 36     while (!isdigit(ch) && ch != '-')    ch = getchar();
 37     bool flag = true;
 38     if ('-' == ch)  flag = false, ch = getchar();
 39     for (res = 0; isdigit(ch); ch = getchar())  res = res * 10 + ch - 48;
 40     if (!flag)  res = -res;
 41 }
 42 void putint(int x) {
 43     int i = 0, a[10];
 44     bool flag = true;
 45     if (x < 0) {
 46         flag = false;
 47         x = -x;
 48     } else if (x == 0) {
 49         a[i++] = 0;
 50     }
 51     while (x > 0) {
 52         a[i++] = x % 10;
 53         x /= 10;
 54     }
 55     if (flag == false)  putchar('-');
 56     for (int j = i - 1; j >= 0; --j)    putchar(a[j] + 48);
 57 }
 58 int mmax(int a, int b)  {return a > b ? a : b;}
 59 int mmin(int a, int b)  {return a < b ? a : b;}
 60 int gcd(int a, int b)   {while (b) b ^= a ^= b ^= a %= b; return a;}
 61 
 62 /*int parent[max_n], rank[max_n];
 63 void ini() {
 64     for (int i = 1; i < 10001; ++i) {
 65         parent[i] = i;
 66         rank[i] = 0;
 67     }
 68 }
 69 int find(int x) {
 70     return x == parent[x] ? x : parent[x] = find(parent[x]);
 71 }
 72 void unite(int x, int y) {
 73     x = find(x);
 74     y = find(y);
 75     if (x == y) return;
 76 
 77     if (rank[x] < rank[y]) {
 78         parent[x] = y;
 79     } else {
 80         parent[y] = x;
 81         if (rank[x] == rank[y]) rank[x]++;
 82     }
 83 }*/
 84 
 85 //#define LOCAL
 86 
 87 int main() {
 88 #ifdef LOCAL
 89     freopen("data.in", "r", stdin);
 90     freopen("data.out", "w", stdout);
 91 #endif // LOCAL
 92     double A[15][35];
 93     int B[15][35];
 94     for (int i = 0; i <= 9; ++i) {
 95         for (int j = 1; j <= 30; ++j) {
 96             double t1 = 1 - pow(2, -i - 1);
 97             int t2 = pow(2, j) - 1;
 98             double t3 = log10(t1) + t2 * log10(2);
 99             B[i][j] = t3;
100             A[i][j] = pow(10, t3 - B[i][j]);
101         }
102     }
103     std::string s;
104     while (cin >> s, s != "0e0") {
105         for (std::string::iterator i = s.begin(); i != s.end(); ++i) {
106             if ('e' == *i) {
107                 *i = ' ';
108                 break;
109             }
110         }
111         std::istringstream ss(s);
112         double a, b;
113         ss >> a >> b;
114         for (int i = 0; i < 10; ++i) {
115             for (int j = 1; j <= 30; ++j) {
116                 if (b == B[i][j] && fabs(a - A[i][j]) < 1e-5) {
117                     cout << i << ' ' << j << endl;
118                 }
119             }
120         }
121     }
122     return 0;
123 }

 

posted @ 2017-03-17 20:34  hit_yjl  阅读(161)  评论(0编辑  收藏  举报