摘要: #include <cstdlib>#include <iostream>#include <algorithm>using namespace std;//如何求解数组中子数组连续元素的之和的最大值 //这种算法应该是最高效的。O(N)复杂度//见于《编程珠玑》P77页 /*分治的方法来求解,对于前i个元素,其最大和要么在前i-1元素中 存数在maxsofar;要么在第i个元素结束,存储在maxendinghere中例如例子中的,当扫描元素6的位置时,其maxendinghere是9,其maxsofar是9;当扫描到-3时,maxendinghere是6, 阅读全文
posted @ 2013-05-30 15:52 夜雨阑珊 阅读(958) 评论(0) 推荐(0) 编辑
摘要: #include <cstdlib>#include <iostream>using namespace std;//全局变量 int arr[] = {0,1,2,4}; //测试数组 int size = sizeof(arr) / sizeof(*arr); //数组元素个数 bool exist(int arr[],int k){ int low = 0, high = size - 1; //设置一个标志,代表是否存在这个值 bool flag=false; //当找到最后或者第一个元素时,是等... 阅读全文
posted @ 2013-05-29 17:52 夜雨阑珊 阅读(639) 评论(0) 推荐(0) 编辑
摘要: /*** 题目:在排序数组中,找出给定数字的出现次数,比如 [1, 2, 2, 2, 3] 中2的出现次数是3次。* 解法:使用二分查找的方法分别找出给定数字的开始和结束位置,最坏情况下时间复杂度为O(logn)*/#include<stdio.h>#include<stdlib.h> //全局变量 int arr[] = {0,1,1,2,2,2,2,4,4,4}; //测试数组 int size = sizeof(arr) / sizeof(*arr); //数组元素个数 int getUpper(int arr[], int key){//获取某个元素最后出现位置 阅读全文
posted @ 2013-05-29 15:50 夜雨阑珊 阅读(2500) 评论(0) 推荐(0) 编辑
摘要: 1 #include <cstdlib> 2 #include <iostream> 3 4 using namespace std; 5 6 /* 7 Name: 8 Copyright: 9 Author: 10 Date: 29/05/13 14:3111 Description: 已排序的数组,求出每个数组的元素出现的次数。 12 */13 14 15 16 int main(int argc, char *argv[])17 {18 //int a[]={1,2,2,2,3,3,4,4,4};19 int a[]={1,2,2,2,3,3... 阅读全文
posted @ 2013-05-29 14:38 夜雨阑珊 阅读(432) 评论(0) 推荐(0) 编辑
摘要: #include <cstdlib>#include <iostream>#include<algorithm>using namespace std;//28/05/13 16:36//如果使用临时数组的话复杂度会减少很多。。。//搞了两个小时终于搞定了//关键在于//1、如何判定那个指针移动//2、数组b遇到最后一个元素时怎么办//3、数组a遇到最后一个元素怎么办//4、设定a的最大值比b的最大值大,就是为了是q指针先打到末尾,要不然很混乱 int main(int argc, char *argv[]){ //初始化数组 int a[]={1,2,3, 阅读全文
posted @ 2013-05-28 16:38 夜雨阑珊 阅读(1886) 评论(0) 推荐(1) 编辑
摘要: #include <cstdlib>#include <iostream>#include<vector>#include <algorithm>using namespace std;//找出vector中后面元素和前面元素的最大差值, //遍历的N2复杂度的就不考虑了//容器从两个元素开始,以此往后取第三个、第四个……元素 //构造最大差值元素 int max_diff(vector<int>& arr){ if(arr.size()<=1) cout<<"容器长度必须是大于2"< 阅读全文
posted @ 2013-05-28 11:47 夜雨阑珊 阅读(880) 评论(0) 推荐(0) 编辑
摘要: #include <cstdlib>#include <iostream>#include <iomanip>#include<fstream>#include<sstream>#include<bitset> #include<string>#include<vector>#include<exception>#include<cassert>using namespace std;int main(int argc, char *argv[]){ //bitset< 阅读全文
posted @ 2013-05-23 17:03 夜雨阑珊 阅读(237) 评论(0) 推荐(0) 编辑
摘要: #include <cstdlib>#include <iostream>using namespace std;//找出因子是2,3,5,的至少一个的第k大数 //辗转相除法 //2013.05.22 PM#include <stdio.h>int isnumber(unsigned long n) //函数判断,是不是只有2、3、5作因子{ unsigned long tempnumber; tempnumber=n; while(tempnumber%2==0 || tempnumber%3==0 || tempnumber%5==0) //循环判断直 阅读全文
posted @ 2013-05-22 16:02 夜雨阑珊 阅读(540) 评论(0) 推荐(0) 编辑
摘要: #include <cstdlib>#include <iostream>using namespace std;//判断一个序列是不是二叉查找树的后序遍历结果//2013.05.22 PMbool isBack(int *p, int begin, int end){ //边界值判断 if(begin>end) return false; //序列为空或者只有一个元素,不用判断,怎样都是真 //if(end==0 ||end==1),这个判断有问题, //return true; if(begin==end) return ... 阅读全文
posted @ 2013-05-22 14:53 夜雨阑珊 阅读(1293) 评论(1) 推荐(0) 编辑
摘要: #include <cstdlib>#include <iostream>using namespace std;int data[]={4,3,2,2,1,1};int t=4;int n=6; int main(int argc, char *argv[]){ for(int i=0;i<6;++i) { if(t==data[i]) cout<<data[i]<<endl; } for(int i=0;i<6;++i){ for(int j=i+1;j<6;++j){ ... 阅读全文
posted @ 2013-05-21 10:22 夜雨阑珊 阅读(1098) 评论(0) 推荐(0) 编辑