题目链接:
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
Input
Output
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
#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(进制转换)
原文地址:http://blog.csdn.net/u012860063/article/details/39156169