C++ <递归求一个数的N次方(仅限一个数的正数次方)>
2015-09-14 16:53
399 查看
注:如果要计算double类型的数据,只需将int改为double即可,如果只是计算整数值的N次方不建议用double类型,
因为double类型只能存储一个数的近似值,所以计算的结果部分时候会有误差。
运行结果:
power.cpp
因为double类型只能存储一个数的近似值,所以计算的结果部分时候会有误差。
运行结果:
Enter a number to the power of another such as (x n): 2 0 The number 2 power of 0 is: 1 Enter a number to the power of another such as (x n): 2 1 The number 2 power of 1 is: 2 Enter a number to the power of another such as (x n): 2 4 The number 2 power of 4 is: 16
power.cpp
#include <iostream> using namespace std; long powerN(int x, int n); int main() { int x, n; cout << "Enter a number to the power of another such as (x n): "; cin >> x >> n; long xn = powerN(x, n); cout << "The number " << x << " power of is: " << xn << '\n'; return 0; } long powerN(int x, int n) { long product = 1; if (n > 0) product = x * powerN(x, --n); return product; }
相关文章推荐
- C#中的递归APS和CPS模式详解
- WinForm实现按名称递归查找控件的方法
- C#中的尾递归与Continuation详解
- C#递归实现显示文件夹及所有文件并计算其大小的方法
- php递归创建目录的方法
- Javascript递归打印Document层次关系实例分析
- oracle 使用递归的性能提示测试对比
- 使用curl递归下载软件脚本分享
- Perl脚本实现递归遍历目录下的文件
- JavaScript的递归之递归与循环示例介绍
- C# 递归查找树状目录实现方法
- 全排列算法的非递归实现与递归实现的方法(C++)
- php递归列出所有文件和目录的代码
- java递归菜单树转换成pojo对象
- 一个JavaScript递归实现反转数组字符串的实例
- Java中的递归详解(用递归实现99乘法表来讲解)
- C语言的递归思想实例分析
- php通过递归方式复制目录和子目录的方法
- php递归法读取目录及文件的方法
- php实现递归与无限分类的方法