题目描述:点击打开链接
值得一提的是,抽象数据类型定义好,然后把函数实现好。
解决代码:
/******************************************************************************/
/* */
/* 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");
}