题解 | #十进制整数转十六进制字符串#
十进制整数转十六进制字符串
https://www.nowcoder.com/practice/80eca5f47e6f473893151b863b25aba1
#include <iostream>
#include <string>
using namespace std;
string toHexString(int n);
int main() {
int n;
cin >> n;
string hexStr = toHexString(n);
cout << hexStr << endl;
return 0;
}
string toHexString(int n) {
// write your code here......
int y;
string s;
while (n > 0) {
y = n % 16;
if (y < 10) s = char('0' + y) + s;
else s = char('A' - 10 + y) + s;
n = n / 16;
}
return s;
}
