poj1001

来源:互联网 发布:羊毛袜品牌 知乎 编辑:程序博客网 时间:2024/06/05 07:20

Exponentiation
Time Limit: 500MS Memory Limit: 10000KTotal Submissions: 137062 Accepted: 33531

Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. 

This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

Sample Input

95.123 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201

#include<stdio.h>#include<string.h>void multiply(int a[],int b[],int c[],int l1,int l2){int i,j,k;int result,yu;for(i = 0;i < 200;i ++)c[i] = 0;for(i = 0;i < l1;i ++){for(j = 0;j < l2;j ++){result = a[i] * b[j];yu = result % 10;result = result / 10;c[i + j + 1] += (c[i + j] + yu) / 10 +result;c[i + j] = (c[i + j] + yu) % 10;}}}int main(){char r[7];int R[2][200],R1[7];int n;int point;//记录小数点后有几位int flag;int len;int l[2];int i,j;char re[200];int p,q;for(i = 0;i < 7;i ++){R1[i] = 0;r[i] = 0;}for(i = 0;i < 2;i ++){l[i] = 0;for(j = 0;j < 200;j ++){R[i][j] = 0;re[j] = 0;}}while(scanf("%s%d",r,&n) == 2){if(n == 0){printf("1\n");continue;}len = 6;point = 0;if(r[0] == '.'){for(i = 6;i > 0;i --)r[i] = r[i - 1];r[i] = '0';len = 7;}for(i = 0;i < len - 1;i ++){if(r[i] == '.'){point = i;break;}}l[0] = 0;for(i = len - 1;i >= 0;i --){if(r[i] == '.')continue;else{R[0][l[0]] = r[i] - '0';R1[l[0] ++] = r[i] - '0';}}if(point != 0)len --;flag = 0;for(i = 1;i < n;){if(i * 2 <= n){multiply(R[flag],R[flag],R[(flag + 1) % 2],l[flag],l[flag]);i = i * 2;l[(flag + 1) % 2] = 2 * l[flag];}else{multiply(R[flag],R1,R[(flag + 1) % 2],l[flag],len);i ++;l[(flag + 1) % 2] = len + l[flag];}flag = (flag + 1) % 2;}if(point != 0){point = (len - point) * n;for(i = 0;i < point;i ++){re[i] = R[flag][i] + '0';}re[i] = '.';while(i < l[flag]){re[i + 1] = R[flag][i] + '0';i ++;}for(p = 0;p < point;p ++){if(re[p] != '0')break;}for(q = l[flag];q > point;q --){if(re[q] != '0')break;}if(q - p != 0){if(re[p] == '.')p ++;else if(re[p] == '0' && re[p - 1] == '.')p = p + 2;for(i = q;i >= p;i --)printf("%c",re[i]);}else if(q - p == 0){printf("0");}printf("\n");}else{for(i = l[flag] - 1;i > 0;i --){if(R[flag][i] != 0)break;}while(i >= 0){printf("%d",R[flag][i]);i --;}printf("\n");}for(i = 0;i < 7;i ++){R1[i] = 0;r[i] = 0;}for(i = 0;i < 2;i ++){l[i] = 0;for(j = 0;j < 200;j ++){R[i][j] = 0;re[i] = 0;}}}return 0;}


0 0
原创粉丝点击