3-12 浮点数 UVa11809

#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	double m, x;
	int ee;
	int E[10][30];
	double M[10][30];
	for (int i = 1; i <= 9; i++)
		for (int j = 0; j <= 30; j++)
		{
			m = 1 - pow(2, -i - 1);
			ee = pow(2, j) - 1;
			x = log10(m) + ee*log10(2);
			E[i][j] = floor(x);
			M[i][j] = pow(10, x - E[i][j]);
		}
	string s;
	while (cin >> s)
	{
		if (s == "0e0")
			break;
		for (string::iterator i = s.begin(); i != s.end(); ++i)
			if (*i == 'e')
				*i = ' ';
		istringstream ss(s);
		double A; int B;
		ss >> A >> B;
		for (int i = 0; i <= 9; i++)
			for (int j = 1; j <= 30; j++)
				if (B == E[i][j] && fabs(M[i][j] - A) < 1e-4)
					cout << i << " " << j << endl;
	}
	return 0;
}
参考https://blog.csdn.net/crazysillynerd/article/details/43339157

猜你喜欢

转载自blog.csdn.net/fuwu4087/article/details/80382154