首页 > 代码库 > POJ1546 & HDU 1335 & ZOJ 1334 Basically Speaking(进制转换)
POJ1546 & HDU 1335 & ZOJ 1334 Basically Speaking(进制转换)
题目链接:
POJ:http://poj.org/problem?id=1546
HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1335
ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=334
Description
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
Mid-Central USA 1995
题意:
给出某一种进制的数,转换为另一种进制!
不管你认为它是不是很简单,反正我被坑了7个小时!
代码如下:
#include <cstdio> #include <cstring> #include <cmath> char s[117], ans[117]; int a, b; int k; int len; int check( char c) { if(c == 'A') return 10; else if(c == 'B') return 11; else if(c == 'C') return 12; else if(c == 'D') return 13; else if(c == 'E') return 14; else if(c == 'F') return 15; else return c-'0'; } char check1(int c) { if(c == 10) return 'A'; else if(c == 11) return 'B'; else if(c == 12) return 'C'; else if(c == 13) return 'D'; else if(c == 14) return 'E'; else if(c == 15) return 'F'; else return c+'0'; } int POW(int a, int len) { int ss = 1; for(int i = 0; i < len; i++) { ss*=a; } return ss; } void slove() { int r, tt = 0; for(int i = 0; i < len; i++) { tt += check(s[i])*pow(a*1.0,len-1-i); } //printf("tt:%d\n",tt); k = 0; while(tt) { r = tt%b; ans[k++] = check1(r); tt/=b; } } int main() { while(scanf("%s",s)!=EOF) { len = strlen(s); scanf("%d %d",&a,&b); slove(); if(k > 7) { printf(" ERROR\n"); continue; } for(int i = 1; i <= 7-k; i++) printf(" "); for(int i = k-1; i >= 0; i--) printf("%c",ans[i]); printf("\n"); } return 0; }
POJ1546 & HDU 1335 & ZOJ 1334 Basically Speaking(进制转换)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。