zoukankan      html  css  js  c++  java
  • 做题记录-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
    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    Scrapy settings 并发数更改
    tp5 规避 [ error ] 未定义数组索引
    967. Numbers With Same Consecutive Differences
    846. Hand of Straights
    1103. Distribute Candies to People
    559. Maximum Depth of N-ary Tree
    1038. Binary Search Tree to Greater Sum Tree
    538. Convert BST to Greater Tree
    541. Reverse String II
    1551. Minimum Operations to Make Array Equal
  • 原文地址:https://www.cnblogs.com/tingxilin/p/11392058.html
Copyright © 2011-2022 走看看