P1017 进制转换

模拟水题,直接上代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
int main() {
	int n, r;
	cin >> n >> r;
	int ans[maxn];
	int tmp = n;
	int pos = 0;
	while(tmp != 0) {
		int cheng = tmp / r;
		int yu = tmp - r * cheng;
		if(yu < 0) cheng++;
		ans[pos++] = tmp - r * cheng;
		tmp = cheng;
	}
	pos--;
	cout << n << '=';
	for(int i = pos; i >= 0; i--) {
		if(ans[i] >= 10) {
			cout << (char)('A' + ans[i] - 10);
		}
		else cout << ans[i];
	}
	cout << "(base" << r << ')';
	return 0;
}

posted on 2016-10-29 16:27  蒟蒻konjac  阅读(101)  评论(0编辑  收藏