软件工程

本类阅读TOP10

·PHP4 + MYSQL + APACHE 在 WIN 系统下的安装、配置
·Linux 入门常用命令(1)
·Linux 入门常用命令(2)
·使用 DCPROMO/FORCEREMOVAL 命令强制将 Active Directory 域控制器降级
·DirectShow学习(八): CBaseRender类及相应Pin类的源代码分析
·基于ICE方式SIP信令穿透Symmetric NAT技术研究
·Windows 2003网络负载均衡的实现
·一网打尽Win十四种系统故障解决方法
·数百种 Windows 软件的免费替代品列表
·收藏---行百里半九十

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
算法之递归系列一

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

题目:

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);
 
}

 

 

 




相关文章

相关软件