【1027 20 进制转换】 Colors in Mars

传送门

题意

给定 \(3\) 个十进制数字 \(a\),表示 RGB 的值,将其分别转换为 \(13\) 进制,即\(0\sim 9;A\sim D\)

数据范围

\(0\leq a\leq 168\)

题解

  • 固定2位数字,需要添加前导 0

Code

#include <bits/stdc++.h>
using namespace std;

string to_str(int x, int radix) {
	string res = "";
	while (x) {
		int t = x % radix;
		res += t < 10 ? t + '0' : t - 10 + 'A';
		x /= radix;
	}
	reverse(res.begin(), res.end());
	return res;
}
int main() {
	int r, g, b; cin >> r >> g >> b;
	string R, G, B; 
	R = to_str(r, 13);
	G = to_str(g, 13);
	B = to_str(b, 13);
	cout << '#' << setfill('0') << setw(2) << R 
	<< setfill('0') << setw(2) << G 
	<< setfill('0') << setw(2) << B;
}
posted @ 2021-02-17 18:25  Hyx'  阅读(60)  评论(0)    收藏  举报