解题思路:
注意事项:
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语言训练-计算:t=1-1/(2*2)-1/(3*3)-...-1/(m*m) (C语言代码)浏览:785 |
兰顿蚂蚁 (C++代码)浏览:1094 |
【出圈】 (C语言代码)浏览:796 |
简单的a+b (C语言代码)浏览:694 |
C语言程序设计教程(第三版)课后习题1.5 (C++代码)浏览:1095 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:465 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:1246 |
2004年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:1341 |
【蟠桃记】 (C语言代码)浏览:1035 |
1128题解(返回值为数组的情况)浏览:474 |