
Algorithms
文章平均质量分 77
jeasn168
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
编程之美4.7 蚂蚁爬杆
《编程之美》4.7节描述了蚂蚁爬杆问题,把所有具体数字都表示成字母后变为形如如下形式的问题:有一根长为L的平行于x轴的细木杆,其左端点的x坐标为0(故右端点的x坐标为L)。刚开始时,上面有N只蚂蚁,第i(1≤i≤N)只蚂蚁的横坐标为xi(假设xi已经按照递增顺序排列),方向为di(0表示向左,1表示向右),每个蚂蚁都以速度v向前走,当任意两只蚂蚁碰头时,它们会同时调头朝相反方向走,速度不变转载 2014-08-30 15:09:38 · 908 阅读 · 0 评论 -
给一个整数数组,有正有负。找出数组最大和,条件是使用的元素不能有相邻
题目:给一个整数数组,有正有负。找出数组最大和,条件是使用的元素不能有相邻输出:1)打印最大和2)打印组成最大和的元素,用空格分隔如果所有元素都是负数,最大和为最小的负数,要求时间复杂度为O(n)例:输入数组:-1,4,5,-2,-6,6输出:115 6原创 2014-08-08 23:46:13 · 1309 阅读 · 0 评论 -
插入排序、二分插入排序、希尔排序、选择排序、冒泡排序、鸡尾酒排序、快速排序、堆排序、归并排序
#include#includeusing namespace std;void print(int a[],int n);void InsertSort(int a[],int n){for(int i=1;i=0 && a[j]>tmp){a[j+1]=a[j];--j;}a[j+1]=tmp;}}void BinInsertSort(int a[],int n){int high;int l原创 2014-07-21 10:12:11 · 602 阅读 · 0 评论 -
八皇后问题使用排列的方法递归解决
#includeusing namespace std;const int N=8;int count=0;void printQueen(int a[],int len){ //输出解 for(int i=0;i<len;++i) { for(int j=0;j<len;j++) { if(a[i]==j+1) cout<<"●"; else原创 2014-06-25 15:19:42 · 834 阅读 · 0 评论 -
将英文句子的单词拆分,并按按字典顺序排序逐个输出单词
#include#include#includeusing namespace std;vector Split(string s,string pattern){string::size_type pos;vector result;s += pattern;for (int i = 0; i {pos = s.find(pattern, i);i原创 2014-04-11 09:51:29 · 7953 阅读 · 0 评论 -
全排列生成算法:next_permutation
概念全排列的生成算法有很多种,有递归遍例,也有循环移位法等等。C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用于为指定序列生成不同的排列。本文将详细的介绍prev_permutation函数的内部算法。按照STL文档的描述,next_permutation函数将按字母表顺序生成给定序列的下一个较转载 2014-09-04 08:56:33 · 10508 阅读 · 2 评论 -
KMP算法和BM算法
#include #include #include using namespace std;int KMPSearch(const string &str,const string &pat){ int len1=str.length(); int len2=pat.length(); if (len1==0 || len2==0 || len1<len2) { retu原创 2014-09-07 21:16:20 · 765 阅读 · 0 评论