华师大 OJ 3024

题目描述:点击打开链接



值得一提的是,抽象数据类型定义好,然后把函数实现好。





解决代码:

/******************************************************************************/
/*                                                                            */
/*  DON'T MODIFY main() function anyway!                                      */
/*                                                                            */
/******************************************************************************/
#include <string.h>
#include <stdio.h>
#include <math.h>


//17:40-->18:19


void solve(); /* write function solve() to process one case of the problem    */
void init(){}
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. 设计新的大整数类型:一共有147位。首先初始化为00000.......00000,一共有147个0
// 2. 将1/8,1/(8^2),   1/(8^3),    1/(8^4),  分别保存起来,所以就要先实现大数的除以8运算。
// 3. 然后设计大数的乘法运算。
// 4. 然后设计大树的加法运算。
// 5. 读入数据,将小数点后面的部分保存在字符串里面。
// 6. 先输出一个“0.”,然后我们接下来把其他的小数部分凑起来。
// 7. 对刚才的字符串进行循环,分别乘以我们已经准备好的1/8,,1/(8^2),   1/(8^3),    1/(8^4),  等等的大数,把他们加到我们的
// 综合的大数里面。
// 8. 把我们的大数输出出来就可以了。




typedef struct {
    int v[147];
} BIGINT;

//  b.v[0]为最高位

BIGINT DIV8(BIGINT * b){   //不会改变b
    BIGINT tmp1,tmp2;
    int k;
    int t;
    int carry;
    // Initialization
    carry = 0;
    tmp1 = *b;

    for(k=0;k < 147; k++){
        t = b->v[k] + carry * 10;
        b->v[k] = t / 8;
        carry = t % 8;
    }
    tmp2 = *b;
    *b = tmp1;
    return tmp2;
}

BIGINT MUL(BIGINT * b, int n){  // 不会改变b
    BIGINT origin, tmp_return;
    int k;
    int carry;
    int t;

    //  initialization
    origin = *b;
    carry = 0;

    for(k = 146;k>=0;k--){
        t = b->v[k] * n + carry;
        carry = t / 10;
        b->v[k] = t % 10;
    }
    tmp_return = *b;
    *b = origin;
    return tmp_return;
}

void ADD(BIGINT * a, BIGINT * b){// 把b加到a上面去,会改变a
    int k;
    int carry;
    int t;

    // initialization
    carry = 0;
    for(k = 146; k>=0; k--){
        t = a->v[k] + b->v[k] + carry;
        a->v[k] = t % 10;
        carry = t / 10;
    }
    if(carry>0) printf("Debug info: carry can not be > 0");
}




void solve(){
    BIGINT T_bigint,tmp_bigint;
    BIGINT base[50];
    int k,i;
    int top;
    char str[52];
    // Initialization
    for(k = 0; k <147;k++){
        T_bigint.v[k] = 0;
        base[1].v[k] = 0;
    }
    base[1].v[0] = 1;
    base[1].v[1] = 2;
    base[1].v[2] = 5;
    for(k = 2;k<=49;k++){
        base[k] = DIV8(&base[k-1]);
    }
    scanf("%s",str);
    printf("0.");
    strcpy(str,&str[2]);
    for(k = 0; k < strlen(str);k++){
        tmp_bigint = MUL(&base[k+1],str[k]-'0');
        ADD( &T_bigint,&tmp_bigint);
    }

    top = strlen(str) * 3 -1;
    while(T_bigint.v[top]==0){
        top--;
    }


    for(i = 0; i<=top;i++){
        printf("%d",T_bigint.v[i]);
    }
    printf("\n");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值