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 |
有點變態,有了數據就好發現錯誤,終於過了搞了幾天晚上,終於體會到測試數據的重要性了,有了數據,一下子就修改好了。 //1001_v002.c // http://poj.org/problem?id=1001 #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_LEN 200 void str_reverse(char * x); void arr_num_to_str(char * result, int * int_result, int len_int_result, int fra_num, int power); int big_multiply(int * sum, int * a, int * b, int la, int lb); int big_power(int * result, int * base_num, int lbase_num, int power); void big_power_master(char * result, char * a, int b); int main(){ char ** p_results = (char **)malloc(sizeof(int *) * MAX_LEN); char * a = (char *)malloc(sizeof(char) * MAX_LEN); // suppose no more than 100 pairs int b = 0; int act_pair_count = 0; int i = 0; for(i = 0; i < MAX_LEN; i++){ p_results[i] = (char *)malloc(sizeof(char) * MAX_LEN); } while(scanf("%s %d", a, &b) == 2 ){ big_power_master(p_results[act_pair_count], a, b); act_pair_count++; } for(i = 0; i < act_pair_count; i++){ printf("%s", p_results[i]); if(i < act_pair_count - 1) printf("\n"); } for(i = 0; i < MAX_LEN; i++){ // free free(p_results[i]); } free(a); free(p_results); return 0; } void big_power_master(char * result, char * a, int b){ int * int_result = (int *)malloc(sizeof(int) * MAX_LEN); int len_int_result = 0; int * tmp_a = (int *)malloc(sizeof(int) * 10); int len_a; int fra_num = 0; int idx_tmp = 0; int i; int first_none_zero_or_point_reach = 0; int miss_num = 0; int dot_have_point = 1; len_a = strlen(a); for(i = len_a - 1; i >= 0; i--){ if(a[i] == '.'){ dot_have_point = 0; break; } } for(i = len_a - 1; i >= 0; i--){ if(a[i] == '.'){ fra_num = len_a - i - 1 - miss_num; first_none_zero_or_point_reach = 1; }else{ if(dot_have_point == 1 || first_none_zero_or_point_reach == 1 || a[i] != '0'){ if(i != 0 || a[i] != '0'){ tmp_a[idx_tmp++] = a[i] - '0'; } first_none_zero_or_point_reach = 1; }else{ miss_num++; } } } // eliminate right zero for(i = idx_tmp - 1; i > 0; i--){ if(tmp_a[i] != 0){ break; }else{ idx_tmp--; } } if(idx_tmp == 0){ // there are no 0^0 result[0] = '0'; result[1] = 0; }else{ len_int_result = big_power(int_result, tmp_a, idx_tmp, b); arr_num_to_str(result, int_result, len_int_result, fra_num, b); } free(tmp_a); free(int_result); } void str_reverse(char * x){ char tmp; int last = strlen(x) - 1; char * p1 = x; char * p2 = x + last; while(p1 < p2){ tmp = *p1; *p1 = *p2; *p2 = tmp; p1++; p2--; } } void arr_num_to_str(char * result, int * int_result, int len_int_result, int fra_num, int power){ int i; memset(result, 0, sizeof(char) * MAX_LEN); for(i = 0; i < len_int_result; i++){ result[i] = int_result[i] + '0'; } if(fra_num != 0){ if(power * fra_num < len_int_result){ for(i = len_int_result - 1; i >= power * fra_num; i--){ result[i + 1] = result[i]; } result[len_int_result + 1] = 0; result[power * fra_num] = '.'; }else{ result[power * fra_num + 1] = 0; result[power * fra_num] = '.'; for(i = len_int_result; i < power * fra_num; i++) result[i] = '0'; } } str_reverse(result); } int big_multiply(int * sum, int * a, int * b, int la, int lb){ int i, j; int lsum = 0; memset(sum, 0, MAX_LEN * sizeof(int)); for(i = 0; i < la; i++){ for(j = 0, lsum = i; j < lb; j++){ sum[lsum] += b[j] * a[i]; lsum++; } } for(i = 0; i < lsum; i++){ if(sum[i] > 9){ sum[i + 1] += sum[i] / 10; sum[i] %= 10; if(i == lsum - 1) lsum++; } } return lsum; } int big_power(int * result, int * base_num, int lbase_num, int power){ int * tmp_result; int i; int len_result = 0; memset(result, 0, MAX_LEN * sizeof(int)); if(power == 0){ result[0] = 1; return 1; }else if(power == 1){ memcpy(result, base_num, lbase_num * sizeof(int)); return lbase_num; }else{ tmp_result = (int *)malloc(sizeof(int) * MAX_LEN); memset(tmp_result, 0, MAX_LEN * sizeof(int)); len_result = big_multiply(result, base_num, base_num, lbase_num, lbase_num); // base_num * base_num = base_num^2 memcpy(tmp_result, result, len_result * sizeof(int)); for(i = 2; i < power; i++){ len_result = big_multiply(result, tmp_result, base_num, len_result, lbase_num); memcpy(tmp_result, result, len_result * sizeof(int)); } free(tmp_result); return len_result; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator