北京大学机试 数制转换 Easy

基本思想:

无;

关键点:

无;

#include<iostream>
#include<string>
using namespace std;
typedef long long ll;


ll tranten(string s,int r) {
	ll cnt = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i]>='0'&&s[i] <= '9') {
			cnt = cnt * r + int(s[i] - '0');
		}
		else {
			cnt = cnt * r + int(s[i] - 'A') + 10;
		}
	}
	return cnt;
}

string tentrans(ll n, int r) {
	string res="";
	if (n == 0)
		return "0";
	while (n != 0) {
		int temp = n % r;
		if (temp >= 10) {
			res =char(temp-10+'A') + res;
		}
		else {
			res = char(temp + '0') + res;
		}
		n /= r;
	}
	return res;
}

int main() {
	string s;
	int a, b;
	while (cin >> a >> s >> b) {
		//消除前导零;
		while (s.size() > 0 && s[0] == '0')
			s.erase(0, 1);
		for (int i = 0; i < s.size(); i++) {
			if (s[i] >= 'a'&&s[i] <= 'z')
				s[i] = (s[i] - 'a') + 'A';
		}
		cout << tentrans(tranten(s,a), b);
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12460097.html