洛谷 P1143 进制转换

洛谷 P1143 进制转换

洛谷 P1143 进制转换_第1张图片

目的:熟悉各种进制转换,理解字符数字转换,学习基本的字符串处理。2019-11-12
//P1143 进制转换
//在线测评地址https://www.luogu.org/problem/P1143
//基本思路,先转成十进制,再转成相应进制,有更好的方法吗。2019-11-12
//样例通过,提交AC.2019-11-12
#include
#include
int n,m,top;
char stack[33],a[33];//a存储读入数据,stack存储转换后的数据
int c2i(char c){//字符转数字
    if('0'<=c&&c<='9')return c-'0';
    if('A'<=c&&c<='F')return c-'A'+10;
}
int s2i(char *s){//字符串转数字
    int i,len,ret=0;
    char c;
    len=strlen(s);
    for(i=0;i     return ret;
}
char i2c(int x){//数字转字符
    if(0<=x&&x<=9)return '0'+x;
    if(10<=x&&x<=15)return x-10+'A';
}
void i2i(int b){//数字转数字
    top=0;
    while(b)stack[++top]=i2c(b%m),b/=m;
}
int main(){
    scanf("%d%s%d",&n,a,&m);
    i2i(s2i(a));
    while(top)printf("%c",stack[top--]);//打印
    printf("\n");
    return 0;
}

你可能感兴趣的:(跟着大佬学算法)