解题思路:
注意事项:
000 = 0
参考代码:
#include <iostream> #include <string> using namespace std; int pow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans *= a; a *= a; b >>= 1; } return ans; } void toOther(int n, int type, string& s) { if (n) { toOther(n/type, type, s); s += n%type > 9 ? n%type - 10 + 'A' : n%type + '0'; } } int toDec(string s, int type) { int len = s.length(); int ans = 0; for (int i = 0; i < len; i++) { if (s[i] >= '0' && s[i] <= '9') ans += (s[i] - '0')*pow(type, len - i - 1); else if (s[i] >= 'A' && s[i] <= 'Z') ans += (s[i] - 'A' + 10)*pow(type, len - i - 1); } return ans; } int main() { string s; cin >> s; if (s == "000") s = "0"; int dec = toDec(s, 16); string oct; if (dec) toOther(dec, 8, oct); else oct = "0"; cout << s << " " << dec << " " << oct << endl; return 0; }
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:601 |
C语言程序设计教程(第三版)课后习题10.1 (C语言代码)浏览:1435 |
C二级辅导-公约公倍 (C语言代码)浏览:1508 |
简单的a+b (C语言代码)浏览:667 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:574 |
printf基础练习2 (有点不明白)浏览:837 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:609 |
用筛法求之N内的素数。 (C语言代码)浏览:664 |
局部变量作函数返回值的问题浏览:965 |
简单的a+b (C语言代码)浏览:533 |