Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
那么我也贴一下我的代码,为啥你们的代码那么短#include <stdio.h> #include <string.h> /* factor[0]被乘数的位数, factor[n:1] 保存被乘数, amplify保存放大倍数 */ void amplify_to_integer(char* base, int factor[], int factor_size, int* amplify) { int i = 0; int j = 0; int tmp = 0; int isdot = 0; *amplify = 0; memset(factor, 0, factor_size); /* 去除小数部分多余的0 */ if(strchr(base, '.')){ for(i = strlen(base) - 1; '0' == base[i]; i--){ base[i] = '\0'; } /* 小数部分全0 */ if('.' == base[i]){ base[i] = '\0'; } } /* 去除整数部分多余的0 */ for(i = 0; '0' == base[i] && base[i+1]; i++); for(j = 1; base[i]; i++){ if('.' == base[i]){ isdot = 1; continue; } factor[j++] = base[i] - '0'; factor[0]++; if(isdot) (*amplify)++; } /* 将因子倒过来 */ for(i = 1; i <= factor[0] / 2; i++){ tmp = factor[i]; factor[i] = factor[factor[0] - i + 1]; factor[factor[0] - i + 1] = tmp; } } // 计算因子的1位和被乘数(multiplicand)的乘积,保存在result里 // pos表示factor是因子中的第几位 void handle_multiply(int *multiplicand, int factor, int *result, int pos) { int i = 0; int val = 0; int t = 0; int carry[256] = {0}; /* 暂时保存进位 */ for(i = 1; i <= multiplicand[0]; i++){ /* 计算2数乘积并计算向高位的进位 */ val = multiplicand[i] * factor; carry[i+1] = val / 10; result[0] = i + pos - 1; /* idx, 个位pos=1,百位pos=2 */ /* 再次计算,若产生进位则再次向高位进位 */ t = val % 10 + carry[i]; result[result[0]] = t % 10; carry[i+1] += t / 10; /* 再次向高位进位 */ } /* 计算完n位数乘积后,判定是否产生第n+1位的进位 */ if(carry[i]){ result[0] = i + pos - 1; result[result[0]] = carry[i]; } } void print_result(int *result, int dotpos) { int i = 0; int bitcnt = result[0]; if(bitcnt < dotpos){ putchar('.'); for(i = 0; i < dotpos - bitcnt; i++){ putchar('0'); } } for(i = result[0]; i > 0; i--){ if(dotpos == i){ putchar('.'); } printf("%d", result[i]); } putchar('\n'); } // result[0] = sum(result[1..rowcnt]) void make_result(int (*result)[256], int rowcnt) { int i = 0; int j = 0; int val = 0; int carry[256] = {0}; for(i = 1; i <= result[rowcnt][0]; i++){ val = carry[i]; for(j = 1; j <= rowcnt; j++){ val += result[j][i]; } result[0][i] = val % 10; /* 计算本位的值 */ carry[i+1] = val / 10; /* 向高位进位 */ } result[0][0] = i - 1; /* 保存结果的长度 */ if(carry[i]){ result[0][0] = i; result[0][i] = carry[i]; } } void make_calc(char* base, int power) { int i = 0; int j = 0; int amplify = 0; /* 放大倍数 */ int factor[6]; /* factor[0]保存数组长度, factor[1:5]保存被乘数各个位 */ int result[6][256]; /* result[0]保存运算结果, result[i][0]保存各行数组长度 */ /* 将底数转换成整数 */ memset(result, 0, sizeof(result)); amplify_to_integer(base, factor, sizeof(factor), &lify); memcpy(result[0], factor, sizeof(factor)); /* x^n, 把x拷贝到result[0] */ for(i = 1; i < power; i++) /* 乘幂运算 */ { /* 处理2个数相乘的情况 */ for(j = 1; j <= factor[0]; j++){ handle_multiply(result[0], factor[j], result[j], j); } make_result(result, factor[0]); /* 修正中间结果长度 */ memset(result[1], 0, sizeof(result[1]) * factor[0]); /* clear buffer */ } /* 输出结果 */ print_result(result[0], (amplify * power)); } int main(int arg, char* argv[]) { char base[256]; int power; /* 底数格式为xx.xx,共占6位 */ while(scanf("%s%d", base, &power) != EOF){ make_calc(base, power); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator