C++且不使用VECTOR头文件完成:当你观察到一个神秘的古老宝箱时你决定尝试打开它。但是宝箱上面的密码是用八进制编码的!幸运的是你是一名优秀的程序员。以下是你需要解决的问题:宝箱上有一行神秘的八进制数字但无法直接读取它。你需要编写一个程序将这个八进制数字转换为十六进制数字以找到正确的密码。输入描述一个八进制数以字符串形式给出。该字符串只包含数0-7、点。输出描述输出一行表示对应的十六进制数以字符
#include
using namespace std;
string octToHex(string oct) { string hex = ""; int len = oct.length(); int dec = 0; int pow = 1; int i = len - 1;
while (i >= 0) {
if (oct[i] != '.') {
dec += (oct[i] - '0') * pow;
pow *= 8;
} else {
hex = '.' + hex;
pow = 1;
}
i--;
}
while (dec > 0) {
int rem = dec % 16;
if (rem < 10) {
hex = char(rem + '0') + hex;
} else {
hex = char(rem - 10 + 'A') + hex;
}
dec /= 16;
}
if (hex == "") {
hex = "0";
}
return hex;
}
int main() { string oct; cin >> oct;
string hex = octToHex(oct);
cout << hex << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/i265 著作权归作者所有。请勿转载和采集!