算法之递归系列一
来源:互联网 发布:从浅深入 mysql 编辑:程序博客网 时间:2024/04/29 21:31
题目:
1、计算 (分治法)
问题描述
对于给定的n,要求在O(n)步内计算出
输入:要计算的n,
输出:
思想:
1. 用普通算法实现计算量为
2. 用分治法实现算法为O(n)级的
分治法具体实现:
利用函数:
F(n)=
程序
//*************************************************************************************
//对于给定的n,要求在O(n)步内计算出2的2的n次幂 ,同时分析该程序的时间复杂性和空间复杂性。
//利用分治法解题
//author:dongkaiying
//use the common method ,we can find that if we use 'double ',the 'n' can only be the
//region lower than 9; so we must find a better method to reduce the complexity of the
//method.We use the "fenzhifa" we called.
//*************************************************************************************
#include<iostream.h>
#include<stdio.h>
double common_M(int n);
double FenZhi_M(int n);
void main()
{
cout<<"Please enter the 'n' you need:"<<endl;
int n;
cin>>n;
//use the easiest method to compute the value;
double j=common_M(n);
cout<<"Use the common method to compute the value is:"<<j<<endl;
double m=FenZhi_M(n);
cout<<"Use another common method to compute the value is:"<<m<<endl;
return;
}
//this method's complexity .:2's n cimi
double common_M(int n)
{
double result=1;
double result_1=1;
for(int x=1;x<=n;x++)
{
result*=2;
}
for(x=1;x<=result;x++)
{
result_1*=2;
}
return result_1;
}
//use the digui method;
double FenZhi_M(int n)
{
if(n==1)
return 4;
else
return FenZhi_M(n-1)*FenZhi_M(n-1);
}
- 算法之递归系列一
- 【算法系列】之递归算法
- 算法之递归(一)
- 每周一算法(之递归算法)
- 数据结构与算法之 二叉树 (一)递归算法
- 算法笔记之 全排列算法 一 递归求解
- 算法笔记之 全排列算法 一 递归求解
- 递归算法学习系列之经典背包问题
- 递归算法学习系列之经典背包问题
- 递归算法学习系列之八皇后问题
- 《算法导论》系列课后思考题之-第四章《递归式》
- 递归算法学习系列之经典背包问题
- 递归算法学习系列之八皇后问题
- 排序系列之二堆排序的非递归算法
- 递归算法学习系列之八皇后问题
- 递归算法学习系列之经典背包问题
- C/C++面试之算法系列--菲波拉契数列的递归与非递归算法
- C/C++面试之算法系列--菲波拉契数列的递归与非递归算法
- 游戏引擎剖析 10
- 游戏引擎剖析 11
- 游戏引擎剖析 1-1
- 我国IT人员创业存在的问题
- 我的一个B/S系统建模
- 算法之递归系列一
- B/S系统建模之修改版
- 使用存储过程时一个错误的解决方法。
- 怎样学好C++
- Visual C++中的ODBC编程实例
- 用VC++实现ODBC数据源设置
- 交大自考报名记
- 在C++ Builder中实现桌布自动更新
- 话说小工具GREP