C++程序练习-1001:Exponentiation

本博客介绍了一个计算任意小数R的N次方的算法,其中R位于0.0到99.999之间,N介于0到25。通过将R转换为整数并进行自我乘法运算,最终得到精确结果并去除不必要的零。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述
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.
输入
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.
输出
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.
样例输入
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
样例输出
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

参考代码//随手写写,计算小数的N次方 //http://poj.grids.cn/practice/1001/ //1.思路清晰,需要耐心,仔细地写 //2.小数转化整数,计算结果后补小数位 //256kB 0ms 2821 B G++ #include <iostream> #include <cstring> using namespace std; //global variables int an1[200],an2[200],anR[400]; //return decimal place,format string,trim zeros int format(char *s){ int i,k,c,len = strlen(s); char t1[6],t2[6]; //decimal number if(strstr(s,".") != NULL){ k = 0; c = 0; for(i = len - 1;i >= 0;i --){ if(s[i] == '.'){ break; } if(s[i] != '0' || k == 1){ k = 1; c ++; } } strncpy(t1,s,i); t1[i] = '\0'; strncpy(t2,s + i + 1,c); t2[c] = '\0'; strcat(t1,t2); for(i = 0;i < strlen(t1);i ++){ if(t1[i] != '0'){ break; } } strncpy(s,t1+i,strlen(t1) - i); s[strlen(t1) - i] = '\0'; return c; }else{ strcpy(t1,s); for(i = 0;i < strlen(t1);i ++){ if(t1[i] != '0'){ break; } } strncpy(s,t1+i,strlen(t1) - i); s[strlen(t1) - i] = '\0'; return 0; } } //multiply void multiply(int len){ int i,j; memset(anR,0,sizeof(anR)); for(i = 0;i < len;i ++){ for(j = 0;j < 200;j ++){ anR[i + j] += an1[i] * an2[j]; } } for(i = 0;i < 200;i ++){ if(anR[i] >= 10){ anR[i + 1] += anR[i] / 10; anR[i] %= 10; } } memcpy(an2,anR,sizeof(an2)); } int main(){ char n[7]; int p,dep,found,len,i,j,flag; while(std::cin>>n>>p){ dep = format(n); memset(an1,0,sizeof(an1)); memset(an2,0,sizeof(an2)); memset(anR,0,sizeof(anR)); len = strlen(n); j = 0; for(i = len - 1;i >= 0;i --){ an1[j ++] = n[i] - '0'; } an2[0] = 1; //start to self multiply dep = dep * p; while(p --){ multiply(len); } //find the decimer place for(i = 199;i >= 0;i --){ if(anR[i] > 0){ found = i; break; } } //print result j = 0; if(dep == found + 1){ std::cout<<"."; j = 1; }else if(dep > found + 1){ std::cout<<"."; j = 1; for(i = 0;i < dep - found - 1;i ++){ std::cout<<"0"; } } flag = 0; for(i = 199;i >= 0;i --){ if(i+1 == dep && j == 0){ std::cout<<"."; } if(anR[i] > 0 || flag == 1){ std::cout<<anR[i]; flag = 1; } } std::cout<<std::endl; } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值