• 做题记录-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
    ",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
    ",temp);
            if(temp/2!=0 || count!=0) shang[count++]=changechar(temp/2);
            temp=temp%2;
            temp=temp*10;
            //printf("cichu:%d
    ",temp);
        }
    
        //printf("ans:%d
    ",temp/10);
        //printf("ha
    ");
        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("
    ");
        }
        return 0;
    }
    View Code
    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    序列化和反序列化
    抽象类与接口
    为了忘却的纪念
    gmail和hotmail也有企业邮局
    tag's tag
    在互联网上裸奔
    音乐网站,可以自己弹琴,歌谱整理
    今天看了ning的介绍,很有意思
    昨天服务器出现问题,解决过程如下所述
    google Trends
  • 原文地址:https://www.cnblogs.com/tingxilin/p/11392058.html
Copyright © 2020-2023  润新知