百练1001:Exponentiation(乘方)(C++实现,北大软微)

该博客介绍了如何使用C++解决北大软微百练1001问题,即计算0.0到99.999之间实数的整数次方。文章提供了输入输出格式、样例及解析,强调了输入时记录小数点位置的重要性,并分享了简化版的代码实现,通过注释帮助理解算法逻辑。
摘要由CSDN通过智能技术生成

百练1001:Exponentiation(乘方)

1-问题描述
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 Rnwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
(翻译:输入一个数R,数值大于0.0且小于99.999;再次输入一个整型数n,数值大于0且小于25,求R的n次方,输出结果。)

2-输入
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.
(翻译:输入两个数R和n,R的数值占1-6列,第7列空,第8-9列为n的数值)

3-输出
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.
(翻译:每个输出结果由一行组成,每一行输入给出R^n的确切值。在输出结果中不要前导零(见于下面输出样例)。不重要的尾随零不能被打印(就是输出数值后面不加0),如果结果是整数,不要打印小数点。)

4-样例输入

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

5-样例输出

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

6-自我分析

输入的时候将小数转换为整数,根据大整数计算原理将概数计算得到结果,这种类型的关键就是输入的时候记录小数点的位置,然后分居计算的次方求出输出时小数点应该输出的位置,其基础还是基本的大数加减乘除。

这道题,我以为是简单的函数调用,结果复杂的一匹,这里借鉴其他博主,然后加以学习,其中优化了源码的各种细节,使得代码更加简洁,并添加注释,一部分注释是原博主的,我则添加详细注释,以便于理解,最后是通过了百练的测试!因为我是学java的,这里c++实现算法只是学习需要,这个算法在考试中价值不大,因为实现的细节比算法本身还繁杂,学习一下代码就好。

7-代码实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 	//该数组存储输入的数值R,7位 
 	char s[7];
 	//len是s[]的长度,其他变量下面再介绍作用		
 	int len, len1, i, j, n, point, num;
 	int ans[200], result[200], a[7], ok, d, m;
  	//乘法函数(与之前用过的模板原理相同)
 	void F(){
   
  	//这里重置i 、j为0,这里是一个复杂的基础计算过程,多次乘法运算,读者自己体会
       	for ( i=0 ; i < len ; i++ )
              for( j = 0 ; j < len1 ; j++ ){
   
                     result
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值