1001 Exponentiation

    技术2022-05-19  36

    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. 题不难。就是麻烦。。 先忽略小数点计算一个值,最后再把小数点加进去、 注意的地方: 1.输出格式;   2.忽略小数上没用的0;  3.不能忽略小数里开头的0,比如说.0023中的00;  4.整数不需要输出小数点;  5.注意输入为0的时候。 程序:  /*poj 1001 exponentiation 2011-2-17*/ #include <stdio.h> #include <math.h> #include <string.h> #define true 1 #define false 0 int n,decimal_num,integer_num,raw_num,max; int result[1000]; char point=true; char raw_data[10]; void multiplication(int a){ int i,carry=0; for(i=1;i<=max;i++){ result[i]= result[i]*a+carry; carry=result[i]/10; result[i]%=10; } while(carry!=0){ max++; result[max]=carry; carry/=10; } } int main(){ int i,j; while(scanf("%s%d",raw_data,&n)==2){ raw_num=0; j=0; point=true; /*point为判断是否有小数点*/ for(i=5;i>=0;i--){ if(raw_data[i]== '.'){ decimal_num= 5-i; /*记录其小数个数*/ continue; } raw_num= raw_num+ (raw_data[i]-'0')*ceil(pow(10,j)); /*raw_num为输入去掉小数点后数值,这样写高精和整型的乘法,个人感觉简单点*/ j++; } decimal_num *=n; /*计算结果的小数个数*/ result[1]=1; max=1; /*结果一共有几个数字*/ for(i=1;i<=n;i++) multiplication(raw_num); i=1; integer_num= max-decimal_num; /*计算整数个数*/ while(result[i]==0){ /*去掉末尾0,这里需要注意,如果结果为10.00,那么整数部分的0不能去,同时,最后输出也不要输出小数点*/ if(i>decimal_num){ point=false; /*point表示是否输出小数点*/ break; } i++; } if(raw_num==0){ /*输入0实在不知道怎么处理。。。直接特判一下。。。*/ printf("0/n"); continue; } if(decimal_num>=max){ /*如果结果为.00023,需要输出小数点后必要的0*/ printf("."); point=false; for(;decimal_num!=max;decimal_num--) printf("0"); } for(;max>=i;max--){ /*输出结果*/ printf("%d",result[max]); integer_num--; if(integer_num==0 && point) printf("."); } printf("/n"); } return 0; }  

    最新回复(0)