HDU 1335 (16进制以下)把m进制转换为n进制

【下题链接: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. 
 

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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eclipse二进制文件是指由Eclipse集成开发环境生成的可执行文件。在Linux下,Eclipse生成的二进制文件通常是没有扩展名的,如上述引用所示。在使用Eclipse编译和运行项目时,如果项目名包含扩展名(例如.hdu.c),则Eclipse可能无正确识别该文件为二进制可执行文件。 这可能导致在运行时出现找不到二进制文件的错误。解决办有两种:一是避免使用带有扩展名的项目名,另一种是手动创建一个运行配置,将命令写死以确保正确识别为二进制文件。 需要注意的是,上述讨论中的EclipseParser库是一个用于验证和读取Eclipse二进制文件内容的DLL库,它是通过按照二进制格式编写的。这个库可以验证和读取Eclipse二进制文件中的EGRID、INIT、UNRST等内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [linux下eclipse c++运行不了提示找不到二进制文件的解决方](https://blog.csdn.net/bjrxyz/article/details/8974483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Eclipse油藏数值模拟软件的二进制文件格式解析](https://blog.csdn.net/slofslb/article/details/119176891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值