zoj 1334
2011-04-02 22:08
225 查看
Basically Speaking
Time Limit: 1 Second Memory Limit: 32768 KB
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-digit 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
Sample Output
Source: Mid-Central USA 1995
这道题也没什么好讲的,很简单,任意进制的转换问题,只是每个人的写法不同。我觉得我这种写法好像有点麻烦了,有木有大牛可以提供一下你们的代码。
Time Limit: 1 Second Memory Limit: 32768 KB
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-digit 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
这道题也没什么好讲的,很简单,任意进制的转换问题,只是每个人的写法不同。我觉得我这种写法好像有点麻烦了,有木有大牛可以提供一下你们的代码。
#include<iostream> #include<iomanip> #include<string> using namespace std; int pow(int a,int b) { if(b==0) return 1; if(b%2==0) return pow(a*a,b/2); else return pow(a*a,b/2)*a; } int main() { string num; int bn,tn; cout.setf(ios::right); while(cin>>num>>bn>>tn) { string tnum=""; int len=num.length(); int mi=0; int oct=0; int pow1=0; for(int i=len;i>0;i--) { pow1=pow(bn,mi++); oct+=((num[i-1]<='9'?(num[i-1]-'0'):((num[i-1]-'A')+10))*pow1); } while(oct) { int temp=oct%tn; tnum=(temp<10?char('0'+temp):char('A'+temp-10))+tnum; oct/=tn; } if(tnum.length()>7) cout<<setw(7)<<"ERROR"<<endl; else cout<<setw(7)<<tnum<<endl; } return 0; }
相关文章推荐
- POJ1546 & HDU 1335 & ZOJ 1334 Basically Speaking(进制转换)
- ZOJ 1334 任意进制转换
- ZOJ Problem Set - 1334 Basically Speaking ac代码及总结
- ZOJ-1334
- poj 1543 & HDU 1334 & ZOJ 1331 Perfect Cubes(数学 暴力大法好)
- 【ACM打卡】ZOJ 1334
- zoj 1334
- ZOJ 1334 Basically Speaking
- zoj 1334 Basically Speaking(进制转换)
- zoj 1334 Basically Speaking
- HDU 1335 || ZOJ 1334 Basically Speaking
- zoj 1334 Basically Speaking
- zoj 1334 进制转换类问题
- ZOJ 1879题解 一道水题没啥好说的,代码贴在这,一开始WA了几次因为题意理解错误,只要报1到n-1每个数都出现一遍就行了
- ZOJ 3686 A Simple Tree Problem
- ZOJ 1006 Do the Untwist
- zoj 2679 Old Bill
- zoj 3195(LCA应用)
- ZOJ 3676 Edward's Cola Plan
- Penney's game (hdu 5169, zoj 3274)