解题思路:
注意事项:
参考代码:
#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() { int dec = 0; while (cin >> dec) { string s; toOther(dec, 8, s); cout << s << endl; } return 0; }
0.0分
0 人评分