【下题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1335】
Basically Speaking
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2906 Accepted Submission(s): 1126
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.
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
【题意】
把十六进制以下的m进制转换为n进制;
【坑点】要求:
1、第一个数是初始值,第二个数是初始进制,第三个数是要求转化的进制。
2、格式(看起来很严格,然而它的初始值居然允许超过7位数字的存在!!)
3、输出必须小于等于七位。否则输出ERROR。
【基本思路】
1、把初始值转化为十进制数。原理:每位数字*初始进制^(位序-1)。
2、把十进制数转化为要求的进制数。原理:除N取余法,如下图
第一次WA的【错误代码】如下:
错误:
#include<stdio.h>
#include<string.h>
#include<math.h>
char str[15];
char str2[30];
void check(char str[]) //使str中的字母转化为数字
{
int i;
for(i=0;i<7;i++)
switch(str[i])
{
case 'A':str[i]='9'+1;break;
case 'B':str[i]='9'+2;break;
case 'C':str[i]='9'+3;break;
case 'D':str[i]='9'+4;break;
case 'E':str[i]='9'+5;break;
case 'F':str[i]='9'+6;break;
}
}
void check2(char str2[]) //使str中的字母转化为数字
{
int i;
int len;
len=strlen(str2);
for(i=0;i<len;i++)
switch(str2[i])
{
case '9'+1:str2[i]='A';break;
case '9'+2:str2[i]='B';break;
case '9'+3:str2[i]='C';break;
case '9'+4:str2[i]='D';break;
case '9'+5:str2[i]='E';break;
case '9'+6:str2[i]='F';break;
}
}
int main()
{
int tenbase;
int jinzhi1,jinzhi2;
while(gets(str)!=NULL)
{
int i,k=0;
tenbase=jinzhi1=jinzhi2=0;
check(str); //使str中的字母转化为数字
for(i=8;i<=9;i++) //初始进制的整数化
if(str[i]!=' ')
{
if(i==8)
jinzhi1=jinzhi1+(str[i]-48)*10;
else
jinzhi1=jinzhi1+str[i]-48;
}
for(i=6;i>=0;i--) //初值转化为十进制
{
if(str[k]!=' ')
tenbase=tenbase+(str[k]-48)*pow(jinzhi1,i);
k++;
}
// printf("%d\n",tenbase);
for(i=11;i<=12;i++) //将要转换的进制整数化
if(str[i]!=' ')
{
if(i==11)
jinzhi2=jinzhi2+(str[i]-48)*10;
else
jinzhi2=jinzhi2+str[i]-48;
}
k=0;
memset(str2,' ',sizeof(str2)); //将输出的数组初始化全为空格
while(tenbase) //除N取余法
{
str2[k]=tenbase%jinzhi2+48;
tenbase=tenbase/jinzhi2;
k++;
}
if(k>7)
printf(" ERROR");
else
{
check2(str2);
for(i=6;i>=0;i--)
printf("%c",str2[i]);
}
printf("\n");
}
return 0;
}
【优化后正确的代码】
#include<stdio.h>
#include<string.h>
#include<math.h>
char str[15];
char str2[30];
void check(char str[]) //使str中的字母转化为数字
{
int i;
for(i=0;i<7;i++)
switch(str[i])
{
case 'A':str[i]='9'+1;break;
case 'B':str[i]='9'+2;break;
case 'C':str[i]='9'+3;break;
case 'D':str[i]='9'+4;break;
case 'E':str[i]='9'+5;break;
case 'F':str[i]='9'+6;break;
}
}
void check2(char str2[]) //使str中的字母转化为数字
{
int i;
int len;
len=strlen(str2);
for(i=0;i<len;i++)
switch(str2[i])
{
case '9'+1:str2[i]='A';break;
case '9'+2:str2[i]='B';break;
case '9'+3:str2[i]='C';break;
case '9'+4:str2[i]='D';break;
case '9'+5:str2[i]='E';break;
case '9'+6:str2[i]='F';break;
}
}
int main()
{
int tenbase;
int jinzhi1,jinzhi2;
while(scanf("%s%d%d",str,&jinzhi1,&jinzhi2)!=EOF)
{
int i,k=0;
tenbase=0;
check(str); //使str中的字母转化为数字
int len;
len=strlen(str);
for(i=len-1;i>=0;i--) //初值转化为十进制
{
if(str[k]!=' ')
tenbase=tenbase+(str[k]-48)*pow(jinzhi1,i);
k++;
}
// printf("%d\n",tenbase);
k=0;
memset(str2,' ',sizeof(str2)); //将输出的数组初始化全为空格
while(tenbase) //除N取余法
{
str2[k]=tenbase%jinzhi2+48;
tenbase=tenbase/jinzhi2;
k++;
}
if(k>7)
printf(" ERROR");
else
{
check2(str2);
for(i=6;i>=0;i--)
printf("%c",str2[i]);
}
printf("\n");
}
return 0;
}