进制转换(进阶)

书接上回,我们可以用10进制作为过渡,把a进制转换成b进制

//a进制转换为b进制 
#include<iostream>
#include<cstring> 
#include<cmath> 
using namespace std;
int main(){
	int a,b;
	cin>>a>>b;
	char s1[100];
	cin>>s1;	
	//1.将a进制的数转化为十进制
	int sum=0;
	for(int i=0;i<strlen(s1);++i){
		int temp;
		if(s1[i]>='0' && s1[i]<='9') //数字字符转化为相对应的数字 
			temp=s1[i]-'0';            
		else if(s1[i]>='A' && s1[i]<='Z') 
			temp=s1[i]-'A'+10;      //大写字母转化为相对应的数字  A===10  B===11...... 
		sum+=temp*pow(a,strlen(s1)-1-i);
	}
	
	//2.将十进制数转化为b进制
	char s2[100];
	int idx=0;
	while(sum>0){
		char temp2;
		if(sum%b>=0 && sum%b<=9)          //余数如果是0-9,就转化为对应的数字字符 
			temp2=sum%b+'0';
		else
			temp2=sum%b-10+'A';           //余数超过10转化为相对应的大写字母 
		s2[
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值