PTA 7-4 小数进制转换 (进制转化)

编程序实现如下功能:输入一个正的十进制纯小数和一个表示进制的整数n(n<10),然后将这个十进制纯小数转换为n进制小数,最后输出结果。

提示:可以先将求得的n进制小数的每一位数存入一个数组中,然后再按符合要求的顺序输出。若不能精确转换,则保留16位小数。
输入格式:

一个正的十进制纯小数,一个整数n(n<10),数据之间以空格分隔。
输出格式:

n进制小数,最多保留16位小数,最后输出换行符。
输入样例:

0.8 2

输出样例:

0.1100110011001100

首先把整数部分直接转化成B进制输出

string num_parse(int a, int b, string& aline) { //把a进制的str转成b进制的str
	vector<int> num;
	string bline;
	for(auto c : aline) {
		if(c >= '0' && c<='9') num.push_back(c - '0');
		else if(c >= 'A' && c <= 'Z') num.push_back(c - 'A' + 10);
		else if(c >= 'a' && c <= 'z') num.push_back(c - 'a' + 36);
	}
	std::reverse(num.begin(), num.end());

	vector<int> res;
	while(num.size()) { //当商为不为0时就循环
		int r = 0; //r是余数
		for(int i=num.size()-1; i>=0; i--) { //模拟b进制除法
			num[i] += r * a;
			r = num[i] % b;
			num[i] /= b;
		}
		res.push_back(r);
		while(num.size() && num.back()==0) num.pop_back();
	}
	for(auto c : res) {
		if(c <= 9) bline += char(c+'0');
		else if(c >= 10 && c <= 35) bline += char(c+'A'-10);
		else if(c >= 36 && c <= 61) bline += char(c+'a'-36);
	}
	reverse(bline.begin(), bline.end());
	return bline;
}

对于小数部分转化过程如下
( 0.8 ) 10 = ( 0.1100110011001100 ) 2 (0.8)_{10}=(0.1100110011001100)_{2} (0.8)10=(0.1100110011001100)2
0.8 2 = 1.6 1 , 0.6 0.8*2=1.6--------得1,小数部分0.6 0.82=1.61,0.6
0.6 2 = 1.2 1 , 0.2 0.6*2=1.2--------得1,小数部分0.2 0.62=1.21,0.2
0.2 2 = 0.4 0 , 0.4 0.2*2=0.4--------得0,小数部分0.4 0.22=0.40,0.4
0.4 2 = 0.8 0 , 0.8 0.4*2=0.8--------得0,小数部分0.8 0.42=0.80,0.8
0.8 2 = 1.6 1 , 0.6 0.8*2=1.6--------得1,小数部分0.6 0.82=1.61,0.6
0.6 2 = 1.2 1 , 0.2 0.6*2=1.2--------得1,小数部分0.2 0.62=1.21,0.2
. . . . . . . . ........ ........
多次操作后可以得到结果
小数部分操作代码如下

string rig = "";
double now = x - (int(x));
K = 16;
while(K-- && now>0.0000000000000000) {
	now *= b;
	rig.push_back('0'+int(now));
	now -= int(now);
}
cout << rig << endl;

完整代码

#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

	char print_f[105];
	void read() {}
	void print() { putchar('\n'); }

	template <typename T, typename... T2>
		inline void read(T &x, T2 &... oth) {
			x = 0;
			char ch = getchar();
			ll f = 1;
			while (!isdigit(ch)) {
				if (ch == '-') f *= -1; 
				ch = getchar();
			}
			while (isdigit(ch)) {
				x = x * 10 + ch - 48;
				ch = getchar();
			}
			x *= f;
			read(oth...);
		}
	template <typename T, typename... T2>
		inline void print(T x, T2... oth) {
			ll p3=-1;
			if(x<0) putchar('-'), x=-x;
			do{
				print_f[++p3] = x%10 + 48;
			} while(x/=10);
			while(p3>=0) putchar(print_f[p3--]);
			putchar(' ');
			print(oth...);
		}
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int n, m, Q, K;

string num_parse(int a, int b, string& aline) { //把a进制的str转成b进制的str
	vector<int> num;
	string bline;
	for(auto c : aline) {
		if(c >= '0' && c<='9') num.push_back(c - '0');
		else if(c >= 'A' && c <= 'Z') num.push_back(c - 'A' + 10);
		else if(c >= 'a' && c <= 'z') num.push_back(c - 'a' + 36);
	}
	std::reverse(num.begin(), num.end());

	vector<int> res;
	while(num.size()) { //当商为不为0时就循环
		int r = 0; //r是余数
		for(int i=num.size()-1; i>=0; i--) { //模拟b进制除法
			num[i] += r * a;
			r = num[i] % b;
			num[i] /= b;
		}
		res.push_back(r);
		while(num.size() && num.back()==0) num.pop_back();
	}
	for(auto c : res) {
		if(c <= 9) bline += char(c+'0');
		else if(c >= 10 && c <= 35) bline += char(c+'A'-10);
		else if(c >= 36 && c <= 61) bline += char(c+'a'-36);
	}
	reverse(bline.begin(), bline.end());
	return bline;
}

#define ld long double

int main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	ld x;
	int b;
	cin >> x >> b;
	string lef = to_string(((int)x));
	cout << num_parse(10, b, lef) << ".";
	string rig = "";
	double now = x - (int(x));
	K = 16;
	while(K-- && now>0.0000000000000000) {
		now *= b;
		rig.push_back('0'+int(now));
		now -= int(now);
	}
	cout << rig << endl;



#ifdef debug
	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}




全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务