随笔分类 -  写程序

摘要:#include #include void heaplify(int *arr, int len, int pos){ int left = pos * 2 + 1; int right = pos * 2 + 2; int max = 0; int temp = 0; if( left arr[pos]) { max = left; } else { max = pos; } if( right arr[max]) { max = right; } if... 阅读全文
posted @ 2014-03-29 23:39 KingsLanding 阅读(458) 评论(0) 推荐(0) 编辑
摘要:#include <iostream>#include <stack>#include <queue>using namespace std;template<class DataType>struct BiNode{ DataType data; struct BiNode *left,*right; BiNode():left(NULL),right(NULL){}};template<class DataType>class BiTree{ public: BiTree() { root = Create();} BiTree( 阅读全文
posted @ 2012-08-25 15:33 KingsLanding 阅读(894) 评论(0) 推荐(0) 编辑
摘要:/* 注意C++ 的内存管理的复杂性 尤其是在merge() 之中,当融合之后如何保证被合并了的链表之后的对象的析构函数会出错,不会被delete两次 还有就是友元函数的模板写法*/#include <iostream>#include <typeinfo>using namespace std;template<class DataType>struct Node{ DataType data; Node<DataType> *next;};template<class DataType>class Linklist;templat 阅读全文
posted @ 2012-08-20 21:06 KingsLanding 阅读(742) 评论(0) 推荐(0) 编辑
摘要:游程编码是对数据压缩的一种方式,这写了一个简单的二值游程编码程序,程序功能如:原始输入:0001110011010100001100 ,压缩之后输出:33221111422也就是相当于记录每个值连续出现的次数,作为编码值。#include <iostream>#include <string>#include <vector>#include <queue>#include <iterator>using namespace std;int main(){ string str("0001110011010100001100 阅读全文
posted @ 2012-06-27 22:28 KingsLanding 阅读(1587) 评论(0) 推荐(0) 编辑
摘要:最近C++程序写的比较少,需要平时多写才行啊。写了一个大整数运算的程序。#include <iostream>#include <vector>#include <cstring>#include <algorithm>#include <ostream>using namespace std;typedef vector<int>::iterator Iter;class bign{ public: bign() { num.clear(); } bign(int n) { ... 阅读全文
posted @ 2012-06-27 21:37 KingsLanding 阅读(3635) 评论(0) 推荐(0) 编辑
摘要:图的m-着色判定问题——给定无向连通图G和m种不同的颜色。用这些颜色为图G的各顶点着色,每个顶点着一种颜色,是否有一种着色法使G中任意相邻的2个顶点着不同颜色?图的m-着色优化问题——若一个图最少需要m种颜色才能使图中任意相邻的2个顶点着不同颜色,则称这个数m为该图的色数。求一个图的最小色数m的问题称为m-着色优化问题。算法思路:color[n]存储n个顶点的着色方案,可以选择的颜色为1到mt=1对当前第t个顶点开始着色:若t>n则已求得一个解,输出着色方案即可否则,依次对顶点t着色1到m, 若t与所有其它相邻顶点无颜色冲突,则继续为下一顶点着色;否则,回溯,测试下一颜色。/*===== 阅读全文
posted @ 2012-06-20 23:01 KingsLanding 阅读(943) 评论(0) 推荐(0) 编辑
摘要:这里介绍的马尔科夫链算法实现的功能是:读入一段英文文本,构造出由这个文本中语言使用情况而形成的统计模型,然后根据统计模型随机输出另一段文本。 马尔科夫链算法的基本思想是:将输入想象成一些相互重叠的短语构成的序列,把每个短语分割为两个部分:一部分是由多个词构成的前缀,另一部分是只包含一个词的后缀。马尔科夫链算法能够生成输出短语的序列,其方法是依据原文本的统计性质,随机地选择跟在前缀后面的特定后缀。采用三个词的短语就能够很好工作,这三个词中前两个词构成前缀来选择作为后缀的一个词设置:w1和w2为文本的前两个词输出w1和w2循环: 随机地选出w3,它是文本中w1w2的后缀中的一个 打印w3 把w.. 阅读全文
posted @ 2012-06-18 22:27 KingsLanding 阅读(10512) 评论(0) 推荐(1) 编辑
摘要:这里主要实现两个线程间通信,当flag = 10 之后通知另外一个线程(也就是“Linux内核多线程(二)”中的程序的各种平台实现)。首先是C++ 11 的方式:#include <thread>#include <iostream>#include <mutex>#include <queue>#include <condition_variable>#include <atomic>using namespace std;const int M = 10;int main(){ mutex lockBuffer; in 阅读全文
posted @ 2012-06-14 23:05 KingsLanding 阅读(2566) 评论(0) 推荐(0) 编辑
摘要:/*基本思想就是将所有结点分成两个集合,一个是st所在的集合,另一个是st不在的集合,每次从不包含st的集合中找出一个最优的结点加入到st所在的集合,并更新st到所有不在其所在集合中的点的距离。使用一个visit[]数组来标识结点在哪一个集合中。使用dist[]数组来记录st结点到其他结点的最短路径。*/#include <iostream>#include <cstdio>#include <cstring>using namespace std;int const MAXN = 1000;const int INF = 1000000000;int g[ 阅读全文
posted @ 2012-05-21 22:33 KingsLanding 阅读(381) 评论(0) 推荐(0) 编辑
摘要:链表合并先,将两个链表排序,在合并的时候引入一个内存泄露的问题,之后会继续讨论。#include <ctime>#include <cstdlib>#include <iostream>#include <cstdio>using namespace std;#define NN#define RANGE 100#define NUM 10struct node{int value;node * next;node(int v = -1, node *nxt = NULL) : value(v),next(nxt){}};node * make_ 阅读全文
posted @ 2012-05-18 22:47 KingsLanding 阅读(2997) 评论(0) 推荐(0) 编辑
摘要:就直接贴代码了:#include <iostream>#include <ctime>#include <cstdlib>using namespace std;#define NN 1struct node{int value;node * next;node(int v = -1, node *nxt = NULL) : value(v),next(nxt){}};node* reverse(node * ); //直接使用 node 代表这个结构体node * make_link();void display(node *);int main(){no 阅读全文
posted @ 2012-05-18 22:37 KingsLanding 阅读(231) 评论(0) 推荐(0) 编辑
摘要:#include <algorithm>#include <vector>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <windows.h>using namespace std;#define SIZE 1024 * 1024 * 1024int main(){LARGE_INTEGER start; LARGE_INTEGER end; LARGE_INTEGER freq; QueryPerformanceFrequency(& 阅读全文
posted @ 2012-05-13 12:35 KingsLanding 阅读(229) 评论(0) 推荐(0) 编辑
摘要:前一篇寻找第k小的数可以用来处理大量数据,这里介绍的堆排序也可以用来处理大量数据的情况,而且堆排序的思想还可以找出前k小的数据(自然也可以找出前k大的数据,就看是建立大根或者小根堆了),只需要建立k个数据的堆就行了,然后依次把后面的数据放入到堆中的适当位置。这里只是实现了堆排序(并没有实现找出前k小的数)。#include <iostream>#include <algorithm>#include <ctime>#include <cstdlib>#include <iterator>using namespace std;cons 阅读全文
posted @ 2012-05-04 19:25 KingsLanding 阅读(264) 评论(0) 推荐(0) 编辑
摘要:使用快排中的partition方法,可以很快找到一个无序序列中的第k小的数。思想:对于一个数组a[0...n-1],分段成a[0...st-1],a[s],a[st+1...n-1]分组后,a[0...st-1]里面的元素都小于等于a[st],a[st+1...n-1]里面的元素都大于等于a[st]. 所以,如果 st==k-1,那么a[st]就是要求的数。如果 st>k-1,那么要求的数在a[0...st-1]里。 如果 st<k-1,那么要求的数在a[st+1...n-1]里。因此我们把范围缩小到 a[0...st-1]或者a[st+1...n-1]里,然后对缩小范围后的数组也 阅读全文
posted @ 2012-05-04 19:17 KingsLanding 阅读(289) 评论(0) 推荐(0) 编辑
摘要:#include <iostream>#include <algorithm>#include <cstdio>#include <cstdlib>#include <ctime>using namespace std;const int NUM = 25;/** 这里默认使用的pivot是a[left] partition将 a[]分成两部分,左边的部分小于pivot,右边的部分大于pivot**////两种partition方式都是对的/*int partition(int a[],int left, int right) //分 阅读全文
posted @ 2012-05-03 23:03 KingsLanding 阅读(322) 评论(1) 推荐(0) 编辑

点击右上角即可分享
微信分享提示