Contest100000579 - 《算法笔记》3.5小节——入门模拟->进制转换

Contest100000579 - 《算法笔记》3.5小节——入门模拟->进制转换

例题

PATB1022

https://pintia.cn/problem-sets/994805260223102976/problems/994805299301433344

//1022 D进制的A+B
题析:注意除基取余法应用,特别注意标注的倒叙输出从i-1开始
以及循环用dowhile(会出现arr[0]=0情况)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
void dec2d(int num,int d)
{
	int arr[50];int i=0;
	do
	{
		arr[i++]=num%d;
		num/=d;
	}while(num != 0);
	for(int j=i-1;j>=0;j--)//从i-1开始而不是i,因为arr[i]==0 
	{
		printf("%d",arr[j]);
	}
}

int main()
{
	int a,b,d;
	while(scanf("%d%d%d",&a,&b,&d) != EOF)
	{
		int sum=a+b;
		dec2d(sum,d);
	}
	return 0;
}

Codeup练习题:

http://codeup.cn/contest.php?cid=100000579

1941 Problem A 又一版 A+B

http://codeup.cn/problem.php?cid=100000579&pid=0

题析:注意事项见注释,还是有坑的包括while(num)while(num!=0)的区别还是不清楚

//1941ProblemA又一版 A+B
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

void dec2m(long long num,int m)
{
	int arr[31];
	int i=0;
	do
	{
		arr[i++]=num%m;
		num/=m;
	}while(num != 0);//此处大坑,while(num)显示错误50%?? 
	for(int j=i-1;j>=0;j--)//此处注意从i-1开始,因为最终一定是arr[i]==0, 
	{					//因为是do...while(区别于while) ,因为有特殊情况(num==0)时 
		printf("%d",arr[j]);
	}
	
}
int main()
{
	long long a,b;
	int m;
	while(scanf("%d%lld%lld",&m,&a,&b)!=EOF)
	{
		if(m==0)
			break;
		dec2m(a+b,m);
		printf("\n");
	}
	return 0;
}


1942 Problem B 数制转换

http://codeup.cn/problem.php?cid=100000579&pid=1
ASCII表

题析:刚开始没弄清题意,后来参考大佬博客
https://blog.csdn.net/privilage/article/details/79959279
主要是字符的处理(将不同进制数作为字符数组,然后转换为10进制)+除基取余法转换的应用
//1942ProblemB数制转换 
#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
	long a,b;
	char num[100];
	char ans[100];
	long y=0;
	//把n进制转换成十进制 
	while(scanf("%ld%s%ld",&a, &num, &b) != EOF)
	{
		long sum=0;
		int len = strlen(num);
		//将a进制转换为10进制
		for(int i=0;i<len;i++)//转换,需熟悉ASCII表 
		{
			if(num[i]>='a')//小写字母转化为大写字母待后续处理
			{
				num[i]=num[i]-32;	
			}	
			//int temp = (num[i] >= 'A'?num[i]-'A'+10:num[i]-'0');
			int temp;
			if(num[i]>='A')
			{
				temp = num[i]-'A'+10;
			}
			else
			{
				temp = num[i]-'0';
			}
			sum = sum*a + temp; 
		} 
		//将10进制转换为b进制,除基取余法
		 int count=0;
		 do
		 {
		 	if(sum%b<=9)
		 	{
		 		ans[count++] = sum%b+'0';
			}
			else
			{
				ans[count++] = sum%b+'A'-10;
			}
			sum/=b;
		 }while(sum != 0);
		 //倒叙输出
		 for(int j=count-1;j>=0;j--)
		 {
		 	printf("%c",ans[j]);	
		 } 
		 printf("\n");
	}	
	return 0;
}


1943 Problem C 进制转换

http://codeup.cn/problem.php?cid=100000579&pid=2



题析:此题经典,见大神解析
https://blog.csdn.net/ActionBeam/article/details/88355452

//1943ProblemC进制转换
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char mod(char num[])//辗转相除法求每一个余数 
{
	int len = strlen(num);
	int rem=0,div=0;
	for(int i=0;i<len;i++)//注意余数为1时要乘以10保留到下一位作为被除数 
	{
		div = ((num[i]-'0')+rem*10) / 2;
		rem = ((num[i]-'0')+rem*10) % 2;
		num[i] = div + '0';//将每一位对应的商放回数组对应的位上
	}
	return rem+'0';//余数转换为字符返回 
}

bool isEmpty(char num[])//判断辗转相除法结束标志 ,即是否待转换数数组全0 
{
	int len=strlen(num);
	for(int i=0;i<len;i++)
	{
		if(num[i]!='0')//注意为字符‘0’ 
		{
			return 0;	
		}	
	}	
	return 1;
} 

char numDec[35];
char numBin[205]; 
int main()
{
	while(scanf("%s",numDec) != EOF)
	{
		int count=0;
		int len=strlen(numDec);
		do
		{
			numBin[count++]=mod(numDec);
		//	cout<<"dadad"<<endl;
		}while(!isEmpty(numDec));
		numBin[count]='\0';
		for(int i=count-1;i>=0;i--)//除基取余法的倒叙输出 
		{
			printf("%c",numBin[i]);
		}
		printf("\n");
		
	}
	return 0;
}


1944 Problem D 八进制

http://codeup.cn/problem.php?cid=100000579&pid=3

题析:题目简单,运用经典的除基取余法即可

//1944ProblemD八进制 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int octal[105];
int main()
{
	int N;
	while(scanf("%d",&N) != EOF)
	{
		int count=0;
		do
		{
			octal[count++]=N%8;
			N/=8;
		}while(N != 0);
		for(int j=count-1;j>=0;j--)
		{
			printf("%d",octal[j]);
		}
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李霁明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值