题解 | #十进制整数转十六进制字符串#
十进制整数转十六进制字符串
http://www.nowcoder.com/practice/80eca5f47e6f473893151b863b25aba1
- 使用algorithm的revers函数反转string 字符串
#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) {
// write your code here......
string ans;
while(n)
{
int m = n%16;
if(m<10)
ans.push_back(char('0'+m));
else
ans.push_back(char('A' + m-10));
n/=16;
}
reverse(ans.begin(), ans.end());//反转字符串
return ans;
}