当前位置:网站首页>m进制数str转n进制数
m进制数str转n进制数
2022-07-26 09:29:00 【逃夭丶】
’0‘ - ’9‘表示:0 - 9
‘a’ - ‘z’ 表示:36 - 61
’A‘ - ’Z‘表示:10 - 35
输入m进制数str,输出n进制数another
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#include<utility>
#include<map>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int INF = 0x3f3f3f3f3f;
const double eps = 1e-6;
const int maxn = 10000010;
char str[maxn],another[maxn];
int ten[maxn];
int switchToTen(int m);
void switchToAnother(int k,int n);
int main()
{
int m,n,k;
cin>>m>>n>>str;
k = switchToTen(m);
switchToAnother(k,n);
return 0;
}
int switchToTen(int m)
{
int i,j,len,k,c;
len = strlen(str);
k = 1;
memset(ten,0,sizeof(ten));
for(int i = 0;i<len;i++){
for(int j=0;j<k;j++){
ten[j] *= m;
}
if(str[i]>='0'&&str[i]<='9'){
ten[0] += str[i]-'0';
}
else if(str[i]>='A'&&str[i]<='Z'){
ten[0] += str[i]-'A'+10;
}
else if(str[i]>='a'&&str[i]<='z'){
ten[0] += str[i]-'a'+36;
}
for(int j=c=0;j<k;j++){
ten[j] += c;
if(ten[j]>=10){
c = ten[j] / 10;
ten[j] %= 10;
}
else c = 0;
}
while(c){
ten[k++] = c % 10;
c /= 10;
}
}
int temp;
for(int i=0,j=k-1;i<j;i++,j--){
temp = ten[i];
ten[i] = ten[j];
ten[j] = temp;
}
return k;
}
void switchToAnother(int k,int n)
{
int sum,r,t,d;
sum = 1;
r = 0;
memset(another,0,sizeof(another));
while(sum){
sum = 0;
for(int i=0;i<k;i++){
d = ten[i] / n;
sum += d;
if(i==k-1){
t = ten[i] % n;
if(t>=0&&t<=9){
another[r] = t+'0';
}
else if(t>=10&&t<=35){
another[r] = t-10+'A';
}
else another[r] = t-36+'a';
r++;
}
else{
ten[i+1] += ten[i]%n * 10;
}
ten[i] = d;
}
}
for(int i=r-1;i>=0;i--){
printf("%c",another[i]);
}
printf("\n");
}
边栏推荐
猜你喜欢
What are CSDN spaces represented by
2022 chemical automation control instrument operation certificate test question simulation test platform operation
PMM(Percona Monitoring and Management )安装记录
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
面试题目大赏
docker配置mysql集群
Windows下Redis哨兵模式搭建
神经网络与深度学习-6- 支持向量机1 -PyTorch
注册模块用例编写
登录模块用例编写
随机推荐
ZXing简化版,转载
asp.net 使用redis缓存
多层嵌套后的 Fragment 懒加载实现
Personality test system V1.0
PMM(Percona Monitoring and Management )安装记录
微信小程序开发
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
字节缓冲流&字符流详解
Elastic APM installation and use
uni-app学习总结
antUI中a-modal 拖拽功能制作
微信小程序学习笔记1
Implementation of fragment lazy loading after multi-layer nesting
安卓 实现缓存机制,多种数据类型缓存
Byte buffer stream & character stream explanation
电机转速模糊pid控制
Solve "note: one or more layouts are missing the layout_width or layout_height attributes."
When you click input, the border is not displayed!
MySQL transaction
暑假第四周