摘要: 还是编程珠玑上的东西,作者对最初的算法稍微优化了一下。#include <iostream>#include <algorithm>#define N 10using namespace std;int binary_search(int *x,int n,int t) //x为要查找的数组,n为数组的大小,t为要找的值,找到则返回值的位置,否则返回-1{ int l=-1; //数组下界 int u=n; //数组上界 int p; //查找的值在数组中的位置 int m; //... 阅读全文
posted @ 2012-11-27 20:54 Dsp Tian 阅读(608) 评论(0) 推荐(0) 编辑
摘要: 好久没看书了,随便拿来《编程珠玑》翻到第8章,介绍了一个叫求数组中最大子数组的算法,是线性的时间复杂度。 问题描述是具有n个浮点数的向量x,求向量中任何连续子向量的最大和。#include <iostream>using namespace std;int main(){ int x[10]={31,-41,59,26,-53,58,97,-93,-23,84}; int maxsofar=0; int maxendinghere=0; for (int i=0;i<10;i++) { maxendinghere=max(maxendinghe... 阅读全文
posted @ 2012-11-27 19:48 Dsp Tian 阅读(539) 评论(1) 推荐(0) 编辑