做题记录-day44

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

C题

这个是爆longlong范围的,需要用到字符串,然后模拟手动除法的过程,短除法之后是倒序输出结果的

ans是在外面定义的,不要在函数里面清空

当0的时候是直接返回的,那么就需要注意到判定条件,只有前面0不计入,后面的是正常计入结果的

#include<string.h>
#include<stdio.h>
int ans[1000];
int countnum=0;
int changenum(char c)
{
    return c-'0';
}
char changechar(int num)
{
    return num+'0';
}
void changetwo(char pre[])
{
    int len=strlen(pre);
    //printf("len:%d\n",len);
    if(len==0) return;
    char shang[1000];
    //memset(ans,0,sizeof(ans));
    memset(shang,0,sizeof(shang));
    //int temp=changenum(pre[0])*10;
    int count=0;
    int temp=0;
    for(int i=0;i<=len-1;i++)
    {
       // printf("i=:%d",i);
        temp=temp+changenum(pre[i]);
        //printf("temp:%d\n",temp);
        if(temp/2!=0 || count!=0) shang[count++]=changechar(temp/2);
        temp=temp%2;
        temp=temp*10;
        //printf("cichu:%d\n",temp);
    }

    //printf("ans:%d\n",temp/10);
    //printf("ha\n");
    ans[countnum++]=temp/10;
    changetwo(shang);
}
int main()
{
    char pre[100];
    while(scanf("%s",pre)!=EOF)
    {
        memset(ans,0,sizeof(ans));
        countnum=0;
        changetwo(pre);
        getchar();
        for(int i=countnum-1;i>=0;i--)
            printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/tingxilin/p/11392058.html