Basically Speaking
The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:
- It will have a 7-digital display.
- Its buttons will include the capital letters A through F in addition to the digits 0 through 9.
- It will support bases 2 through 16.
Input
The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the end of file is reached.
Output
The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is to large to appear on the display, then print ERROR'' (without the quotes) right justified in the display.
Sample Input
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15
Sample Output
120
78
1765
7CA
ERROR
11001
12D687
D071
Source
题目类型:简单进制转换
算法分析:直接进行任意进制之间的转换即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 166; int ans[maxn], len; char input[maxn]; int TransOld (char *val, int base) { int i, sum = 0, wei = 1, len = strlen (val); for (i = len - 1; i >= 0; i--) { if (val[i] >= 'A' && val[i] <= 'Z') sum += (val[i] - 'A' + 10) * wei; else sum += (val[i] - '0') * wei; wei *= base; } return sum; } void Trans (int val, int base) { len = 0; do { ans[len++] = val % base; val /= base; } while (val); } int main() { // ifstream cin ("aaa.txt"); int oldbase, newbase; while (cin >> input >> oldbase >> newbase) { int temp = TransOld (input, oldbase); Trans (temp, newbase); if (len > 7) { cout << " ERROR" << endl; continue; } int i; for (i = 1; i <= 7 - len; i++) cout << " "; for (i = len - 1; i >= 0; i--) { if (ans[i] > 9) cout << char (ans[i] - 10 + 'A'); else cout << ans[i]; } cout << endl; } return 0; } |
- « 上一篇:poj1528
- poj1555:下一篇 »