随笔分类 -  算法导论

摘要:根据如上公式,可以写出O(lgn)的算法: 1 int Power(int num,int index) 2 { 3 if(index==1) 4 return num; 5 if(index%2==0) 6 { 7 return Power(... 阅读全文
posted @ 2014-04-24 12:57 CrazyCode. 阅读(427) 评论(0) 推荐(0)
摘要:首先分成两个容器.第一个容器就是装有生成树里面的顶点,第二个容器就是装有没有放入这个第一个容器中的顶点.首先默认往第一个容器里面装一个顶点.然后..计算出第二个容器里所有顶点和这个顶点的距离.没有连线的设置为无穷大.然后要计算出第二个容器中的顶点与第一个容器的最短距离.(也就是说每往第一个容器中插入... 阅读全文
posted @ 2014-04-22 21:33 CrazyCode. 阅读(163) 评论(0) 推荐(0)
摘要:1 #include"stdafx.h" 2 #include 3 using namespace std; 4 /* 5 动态规划算法: 6 1.刻画一个最优解的结构特征 7 2.递归地定义最优解的值 8 3.计算最优解的值,通常采用自底向上的方法 9 4.利用计算出的信息构造一个最优解. 10 */ 11 /*普通递归法*/ 12 int CutRod(int *p ,int n) 13 { 14 if(n==0) 15 return 0; 16 int q=0; 17 for(int i = 1;i=0) 38 ... 阅读全文
posted @ 2014-04-05 17:08 CrazyCode. 阅读(352) 评论(0) 推荐(0)
摘要:最大子数组问题:找出数组中和最大的最大的非空连续子数组.这里定义一个ret的类来进行存储三个值.用public只是用来方便读取和使用..#include "stdafx.h"#includeusing namespace std;class ret{public:ret(void){};ret(in... 阅读全文
posted @ 2013-10-07 02:29 CrazyCode. 阅读(173) 评论(0) 推荐(0)
摘要:分治法的思想:将原问题分解为几个规模较小但类似于原问题的子问题,递归地求解这些子问题,然后再合并这些子问题的解来建立原问题的解。 1 2 #include "stdafx.h" 3 #include 4 using namespace std; 5 6 void Merge(int *arr,int p ,int q ,int r);//归并排序 7 8 void mergesort(int *arr,int p,int r); 9 void ViewData(int *,int len);10 int _tmain(int argc, _TCHAR* argv[])11 阅读全文
posted @ 2013-10-06 00:54 CrazyCode. 阅读(223) 评论(0) 推荐(0)
摘要:插入排序:对于少量元素的排序,是一个比较有效的算法。#include "stdafx.h"#includeusing namespace std;void Insertion_Sort(int *,int );//插入升序排序void Insertion_SortDown(int *,int );//插入降序排序void ViewData(int *,int len);//读数据int _tmain(int argc, _TCHAR* argv[]){int arr[]={5,2,12,34,2,4,6,1,3};size_t len=sizeof(arr)/sizeof( 阅读全文
posted @ 2013-10-06 00:49 CrazyCode. 阅读(151) 评论(0) 推荐(0)