华师大 OJ 3031

来源:互联网 发布:彼岸花开网络歌手 编辑:程序博客网 时间:2024/04/28 00:19

题目链接:点击打开链接



值得一提的是,我觉得这道题目是我第一次完全靠自己的力量,按照数据结构的思路做出来的。

其实换成数据结构+算法的思路在用C语言来解决问题还是能简化思考的复杂度。

因为人的脑子很笨,无法一下子解决一团问题,需要将其分解。

那么设计好这里的数据结构,其实运行在数据结构上的算法也是非常的重要的。正是有了那些小小的配套的方法,才让思考的过程显得格外的清晰。


解决方案

/******************************************************************************//*                                                                            *//*  DON'T MODIFY main() function anyway!                                      *//*                                                                            *//******************************************************************************/#include <stdio.h>#include <string.h>//把个位数放在v[0],十位数放在v[1]//特殊数据:数据0,表示为{0,{0}},typedef struct {    int cnt;    int v[101];} BIGINT;void solve(); /* write function solve() to process one case of the problem    */void init(){}void ADD01(BIGINT *n,int bin);void MUL2(BIGINT *n);void DIV2(BIGINT * n);int main(){  int i,t; init();   scanf("%d\n",&t);   for (i=0;i<t;i++)   { printf("case #%d:\n",i);     solve();   }   return 0;}//1. 十进制n-->二进制数bin//2. 二进制bin倒置-->binReverse//3. binReverse-->倒置//要自己设计大整数类型void solve(){  //10    char str[102];    int bin[334];    int countBin;    int flag;    BIGINT n;    int i;    scanf("%s",str);    if(strlen(str)==1 && str[0] == '0'){ printf("0\n");return;}    for(i=0;i<strlen(str);i++){        n.v[i] = str[strlen(str) - 1 -i]-'0';    }    n.cnt = strlen(str);    countBin = 0;    while(n.cnt>0){        bin[countBin++] = n.v[0] % 2;   //bin[0]存放的是原本的二进制的最低位        DIV2(&n);                       //bin一共有countBin个二进制位,位bin[0],bin[1],...,bin[countBin-1]    }    flag = 0;    for(i=0;i < countBin;i++){        if(bin[i] != 0)   break;    }    flag = countBin-1;    while(flag){        if(bin[flag]!=0){            break;        }        else flag--;    }    for(;i <=flag ;i++){        MUL2(&n);        ADD01(&n,bin[i]);    }    for(i=0;i<n.cnt-1;i++){        printf("%d",n.v[n.cnt - 1 - i]);    }    printf("%d\n",n.v[n.cnt - 1 - i]);}//大整数加0 or 1//大整数乘以2//大整数除以2void ADD01(BIGINT *n,int bin){    int i;    int t;    int carry;    if(bin!=0){  //bin == 1        if(n->cnt == 0) n->cnt=1,n->v[0] = 1;        else{            carry = bin;            for(i=0;i < n->cnt; i++){                t = n->v[i] + carry;                n->v[i] = t % 10;                carry = t / 10;            }            if(carry>0){                n->v[n->cnt++] = carry;            }        }    }}void MUL2(BIGINT *n){    int i;    int t;    int carry;    if(n->cnt != 0){        carry = 0;        for(i=0;i<n->cnt;i++){            t = n->v[i] * 2  + carry;            n->v[i] = t % 10;            carry = t / 10;        }        if(carry>0){            n->v[n->cnt++] = carry;        }    }}void DIV2(BIGINT * n){ //  {2,{3,4} }    int i;    int t;    int cnt = n->cnt;    int carry;    if(n->v[n->cnt-1] == 1){        carry = 1;        n->cnt--;    } else{        t = n->v[n->cnt-1];        n->v[n->cnt-1] = t / 2;        carry = t % 2;    }    for(i=cnt-2;i>=0;i--){        t = ( n->v[i] + carry*10 );        n->v[i] = t / 2;        carry = t % 2;    }}


0 0
原创粉丝点击