C++ 十进制转十六进制:高效算法实现
#include\x20
using\x20namespace\x20std;
string\x20decimalToHexadecimal(int\x20n)\x20{ \x20\x20string\x20hexadecimal\x20=\x20""; \x20\x20while\x20(n\x20>\x200)\x20{ \x20\x20\x20\x20int\x20remainder\x20=\x20n\x20%\x2016; \x20\x20\x20\x20if\x20(remainder\x20<\x2010)\x20{ \x20\x20\x20\x20\x20\x20hexadecimal\x20=\x20to_string(remainder)\x20+\x20hexadecimal; \x20\x20\x20\x20}else\x20{ \x20\x20\x20\x20\x20\x20char\x20digit\x20=\x20'A'\x20+\x20remainder\x20-\x2010; \x20\x20\x20\x20\x20\x20hexadecimal\x20=\x20digit\x20+\x20hexadecimal; \x20\x20\x20\x20} \x20\x20\x20\x20n\x20/=\x2016; \x20\x20} \x20\x20return\x20hexadecimal; }
int\x20main()\x20{ \x20\x20int\x20n; \x20\x20cin\x20>>\x20n; \x20\x20cout\x20<<\x20decimalToHexadecimal(n)\x20<<\x20endl; \x20\x20return\x200; }
原文地址: https://www.cveoy.top/t/topic/pPL4 著作权归作者所有。请勿转载和采集!