您的位置:首页 > 其它

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

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: