HDU 1335 Basically Speaking(进制转化)

来源:互联网 发布:javascript百度云网盘 编辑:程序博客网 时间:2024/05/29 05:09



 Basically Speaking
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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-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 101111000 2 162102101 3 102102101 3 15 12312 4 2 1A 15 21234567 10 16 ABCD 16 15
 

Sample Output

120 78 1765 7CA ERROR 11001 12D687 D071

题目大意:

给出一个数,把它转成另一个进制。

解题思路:

先把它转为十进制,后转化为目标进制。


代码:

#include<iostream>#include<cstdio>#include<string>#include<vector>#include<cmath>using namespace std;string str;int convertFrom,convertTo;long long sum=0;vector <char> v;void convert10Base(){    int mark=0;    for(int i=str.length()-1;i>=0;i--){        if('0'<=str[i]&&str[i]<='9'){//强制转换要加1e-7,不然会出现精度流失。比如样例的1234567中,百位的5就会因为失去精度而表示499.cout<<temp<<endl;            sum=sum+(int)(str[i]-'0')*pow(convertFrom,mark++)+1e-7;//乘方要用pow(a,b)函数,不能用a^b,被坑了很久。        }        else{            sum=sum+(int)(str[i]-'A'+10)*pow(convertFrom,mark++)+1e-7;        }    }}void solve(){    int mark=6,temp;    if(sum/pow(convertTo,6)>=convertTo) printf("  ERROR");    else {        for(int i=0;i<7;i++){            temp=sum/pow(convertTo,mark--);            if(0<=temp&&temp<=9){                v.push_back((char)(temp+'0'));//pow赋值给int的时候:指数用常量,结果正确;指数用int、double,结果错误                sum=sum-temp*pow(convertTo,mark+1)+1e-7;// pow赋值给double的时候,指数用常量、int、double,结果都正确            }            else{                v.push_back((char)(temp+'A'-10));                sum=sum-temp*pow(convertTo,mark+1)+1e-7; //此题若把sum定义成double型也可避免精度缺失            }        }    }        vector <char>::iterator it;        for(it=v.begin();it!=v.end();it++){            if(*it!='0') break;            else *it=' ';        }        for(int i=0;i<v.size();i++) cout<<v[i];        cout<<endl;        sum=0;}int main(){    while(cin>>str>>convertFrom>>convertTo){        convert10Base();        v.clear();        solve();    }    return 0;}



1 0
原创粉丝点击