首页 > 其他 > 详细

北京大学机试 数制转换 Easy

时间:2020-03-11 10:35:59      阅读:56      评论:0      收藏:0      [点我收藏+]

基本思想:

无;

 

关键点:

无;

 

#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;
}

  

北京大学机试 数制转换 Easy

原文:https://www.cnblogs.com/songlinxuan/p/12460097.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!