Basically Speaking
http://acm.hdu.edu.cn/showproblem.php?pid=1335
Problem 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 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
输入 s n m
题意:把输入的第一个数s为n进制,转化为m进制
# include <iostream>
# include <cstdio>
# include <cstring>
# include <cmath>
int f(char *ch,int n,int m);//n-->10进制
void f1(int s,int n);//10--〉n进制
char st[10009];
char str[]="0123456789ABCDEF";
int main(){
char ch[1000];
int n,m,i,j;
while(scanf("%s%d%d",ch,&n,&m)!=EOF){
int s = f(ch,n,10);
f1(s,m);
int j = strlen(st)-1;
int i = 0;
char t;
while(i<j){
t = st[i];
st[i] = st[j];
st[j] = t;
i++;
j--;
}
int len = strlen(st);
if(len>7) printf("%7s\n","ERROR");
else printf("%7s\n",st);
}
return 0;
}
int f(char ch[],int n,int m){
int sum = 0;
int len = strlen(ch);
int cnt = 0;
for(int i=len-1;i>=0;i--){
if(ch[i]>='0'&&ch[i]<='9') sum+=(ch[i]-'0')*pow(n,cnt);
else if(ch[i]>='A'&&ch[i]<='F') sum+=(ch[i]-'A'+10)*pow(n,cnt);
cnt++;
}
return sum;
}
//10--n进制
void f1(int s,int n){
memset(st,0,sizeof(st));
int cnt = 0;
while(s!=0){
st[cnt] = str[s%n];
s = s/n;
cnt++;
}
}
代码 2:
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
int powe(int,int);
int main()
{
string s;
short b,e;
while(cin>>s>>b>>e)
{
vector<char> v;
string str="";
int sum=0;
for(int i=s.size()-1,c=0;i>=0;--i,++c)
{
if(s[i]>='0'&&s[i]<='9')
sum+=(s[i]-48)*powe(b,c);
if(s[i]>='A'&&s[i]<='F')
sum+=(s[i]-55)*powe(b,c);
}
int r;
while(sum!=0)
{
r=sum%e;
if(r>=0&&r<=9)
v.push_back(r+48);
else if(r==10)
v.push_back('A');
else if(r==11)
v.push_back('B');
else if(r==12)
v.push_back('C');
else if(r==13)
v.push_back('D');
else if(r==14)
v.push_back('E');
else
v.push_back('F');
sum/=e;
}
for(int i=v.size()-1;i>=0;--i)
str+=v[i];
if(str.size()>7)
cout<<setw(7)<<right<<"ERROR"<<endl;
else
cout<<setw(7)<<right<<str<<endl;
}
return 0;
}
int powe(int m,int n)
{
int sum=1;
while(n--)
sum*=m;
return sum;
}