题解 |

十进制整数转十六进制字符串

http://www.nowcoder.com/practice/80eca5f47e6f473893151b863b25aba1

#include #include #include <stdlib.h> #include #include #include // std::reverse 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......

//fun1
//string str;
//char ch[32];
//sprintf(ch,"%X",n);
//str = ch;
//return str;

//fun2
//stringstream ss;
//ss<<setiosflags(ios::uppercase)<<hex<<n;
//string str;
//ss>>str;

//fun3

string str=""; while(n!=0) { int temp = n%16; if(temp>=0 && temp<=9) { str+=temp+'0'; } else { str+=temp -10 +'A'; } n/=16; }

reverse(str.begin(),str.end()); return str;

}

昆· 希斯莱杰 文章被收录于专栏

学习ing,分享更多优质题解。

全部评论

相关推荐

牛客73617529...:无端端被你骂一句
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务