POJ 1001(Exponentiation)

来源:互联网 发布:美国网络存储公司 编辑:程序博客网 时间:2024/05/16 10:53



Exponentiation
Time Limit: 500MS Memory Limit: 10000KTotal Submissions: 79498 Accepted: 18840

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

Hint

If you don't know how to determine wheather encounted the end of input: 
s is a string and n is an integer 
答案:

#include <stdio.h>#include <string.h>int result[126] = {0};//5 * 25 = 125int len;void mul (int a[], int n) {int i ;int add = 0;//相乘后增加的for ( i = 0; i < len; i++) {int term = a[i] * n + add;a[i] = term % 10;add = term / 10;}while (add) {a[i++] = add % 10;add = add / 10;}len = i;//长度可能有变化}int main () {char a[6];int a1, r;while (scanf("%s %d", a, &r) != EOF) {int position = 0, sum = 0, len1;//position记录小数点的位置len1 = strlen(a);for ( int i = 0; i < len1; i++) {//将浮点数转化为整数if (a[i] == '.') {position = (len1 - i - 1) * r;i++;}sum = sum * 10 + a[i] - 48;}result[0] = 1;len =  1;for (int i = 0; i < r; i++) {mul(result, sum);}//增加小数点if (len <= position) {printf(".");//补零for (int i = 0; i < position - len; i++) {printf("0");}int k = 0;//去掉无效0while (result[k] == 0) {k++;}for (int i = len - 1; i >= k; i--) printf("%d", result[i]);printf("\n");}else {int j = 0;// 去掉无效0while (result[j] == 0 && j < position) j++;for (int i = len - 1; i >= j; i--) {if (i + 1 == position)printf(".");printf("%d", result[i]);}printf("\n");}}}

Exponentiation
Time Limit: 500MS Memory Limit: 10000KTotal Submissions: 79498 Accepted: 18840

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

Hint

If you don't know how to determine wheather encounted the end of input: 
s is a string and n is an integer 
0 0
原创粉丝点击