3438.数制转换(AcWing)

在这里插入图片描述

思路

因为会有可能转化成的数值是超过十进制的,所以先声明一个char类型数组,然后到时候避免出现超过十进制的数字,直接输出对应的数组类型的数字

AC代码
#include<iostream>
#include<string>
#include<stack>
#include<algorithm>
using namespace std;
char num[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

void ans(int a, string n , int b)
{
	int res=0,base=1,t=0;
	for(int i = n.length()-1; i>=0; i--)
	{
		t=0;
		if(isdigit(n[i])) t=n[i]-'0';
	else
	{
		if(n[i]>='a') t=n[i]-'a'+10;
		else t=n[i]-'A'+10;	
	}
		res+=t*base;
		base=base*a;
	}
	stack<char>s;
	while(res)
	{
		s.push(num[res%b]);
		res/=b;
	}
	while(!s.empty())
	{
		cout<<s.top();
		s.pop();
	}
	cout<<endl;
}

int main()
{
	int a, b;
	string n;
	cin>>a>>n>>b;
	ans(a,n,b);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值