C++程序练习-1001:Exponentiation

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

描述
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; }

您可能感兴趣的与本文相关的镜像

Dify

Dify

AI应用
Agent编排

Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中

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 小提示 If you don't know how to determine wheather encounted the end of input: s is a string and n is an integer C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... } 来源 East Central North America 1988 北大OJ平台(代理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值