可用最基础的算法,辗转相除法貌似叫。最后reverse一下
#include <iostream>
#include <string>
#include<algorithm>
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) {
string str=" ";
while(n>0)
{
int m=n%16;
if(m>=0&&m<=9)
str+=m+'0';
else
str+=m-10+'A';
n/=16;
}
reverse(str.begin(),str.end());
return str;
}