Do title records -day44

"Algorithm Notes" Section 3.5 - Getting Started Analog -> hex conversion

C title

This process is longlong explosive range, it is necessary to use a string, then the analog division manually, after a short reverse output of the divider is

ans is defined on the outside, not inside empty in the function

When 0 is returned directly, then it is determined that conditions need to be aware that only the front of 0 is not included, the latter is normally included in the results

#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

 

Guess you like

Origin www.cnblogs.com/tingxilin/p/11392058.html