• 做题记录-day43


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

    B题

    因为涉及到了大于十进制的部分需要用到字符串,在写的过程中出现了一点问题

    最开始的时候,在tostring函数里面return的是局部变量,这时候在函数结束变量就消失,是不对的,(要么定义全局要么static)

    注意因为static的存在,每次都要初始化数组,不然自己不会随着函数的结束而消失

    其次,指针是需要初始的,如果仅定义char*p,赋值p几是错误的

    要char c;char a[100];p=&c或者p=a才可以

    还有就是注意,可以使用memset,但是sizeof(p)的大小固定就是4(无论什么类型,只要是指针就是4,因为地址占用4字节,如果是(sizeof(*p)),这时候里面是char对应的大小,这是不对的,要sizeof(a)才可以

    #include<stdio.h>
    #include<string.h>
    void tolower(char n[])
    {
        int len=strlen(n);
        for(int i=0;i<=len-1;i++)
        {
            if(n[i]>='a' && n[i]<='z')
            {
                n[i]=n[i]-'a'+'A';
            }
        }
    }
    int tonum(char c)
    {
        if(c>='0' && c<='9')
        {
            return c-'0';
        }
        return c-'A'+10;
    }
    char tochar(int n)
    {
        if(n>=0 && n<=9)
            return n+'0';
        return n-10+'A';
    }
    int toten(int a,char n[])
    {
        int len=strlen(n);
        int sum=0;
        int total=1;
        for(int i=0;i<=len-1;i++)
        {
            sum+=tonum(n[len-1-i])*total;
            total=total*a;
        }
        return sum;
    }
    char*tostring(int b,int num)
    {
        int ans[1000];
        static char*anschar;
        static char tempc[1000];
        anschar=tempc;
        memset(anschar,0,sizeof(anschar));
        int count=0;
        do
        {
            ans[count++]=num%b;
            num=num/b;
            //printf("num:%d
    ",num);
        }while(num!=0);
        //printf("count:%d
    ",count);
        for(int i=0;i<=count-1;i++)
        {
            anschar[i]=tochar(ans[count-i-1]);
           // printf("%c ",anschar[i]);
        }
       // printf("count:%d
    ",count);
       // printf("len:%d
    ",strlen(anschar));
        //printf("%c
    ",ans[count]);
        //anschar[count]='';
        return anschar;
    }
    int main()
    {
        int a,b;
        char n[1000];
        while(scanf("%d",&a)!=EOF)
        {
            getchar();
            scanf("%s %d",n,&b);
            tolower(n);
            int num=toten(a,n);
            char*str=tostring(b,num);
            printf("%s
    ",str);
            //printf("haha");
        }
        return 0;
    }
    View Code

    字符数组和字符指针都是非常需要注意的,他们的same也是需要自己写的

    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    iOS热更新-8种实现方式
    HTTPS分析-简单易懂
    猖獗的假新闻:2017年1月1日起iOS的APP必须使用HTTPS
    iOS的ATS配置
    Objective-C中block的底层原理
    iOS系列文章
    UIViewController生命周期-完整版
    缩放因子和UI设计
    iOS APP 如何做才安全
    逆向工程
  • 原文地址:https://www.cnblogs.com/tingxilin/p/11386188.html
Copyright © 2020-2023  润新知