- 博客(30)
- 收藏
- 关注
原创 矩阵链乘法(动态规划法)
//动态规划法 //矩阵链乘法问题#include <iostream> #include <limits.h>using namespace std;int p[1000]; int m[1000][1000],s[1000][1000];void SEARCH(int s[][1000],int start,int end) { if(start > end) {cout << "wro
2017-08-03 14:58:35
453
原创 斐波那契数(动态规划法)
1.递归方法//斐波那契数 #include <iostream>using namespace std;int F[1000];long long FIB(int n) { if(n == 0) return 0; if(n == 1) return 1; else return FIB(n-1)+FIB(n-2); }int main(int argc, char *ar
2017-08-01 16:29:09
324
原创 201609-1
#include <iostream> #include <math.h> using namespace std;int main(int argc, char *argv[]) { int first,second,max; max=0; int n; cin >> n; cin >> first; for(int i=2;i<=n;i++) {
2017-08-01 09:09:38
212
原创 201503-2
#include <iostream> #include <cstring> using namespace std;void SORT(int A[],int length) { for(int i=2;i<length;i++) { int key=A[i]; int j=i-1; while(j>0 && A[j]<key)
2017-08-01 09:09:15
184
原创 201503-1
#include <iostream>using namespace std;int A[1001][1001];int main(int argc, char *argv[]) { int m,n; cin >> m >> n; for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) cin >> A[i][j];
2017-08-01 09:08:41
191
原创 201412-2
#include <iostream> #define N 501 using namespace std;void PRINT(int A[][N],int sum,int n) { int i,j; if(sum > n+1) { i=sum-n; j=n; } else { i=1;j=sum-i;
2017-08-01 09:07:39
187
原创 活动选择问题(贪心法)
1.递归算法//贪心法求解活动选择问题 //自顶向下递归贪心算法#include <iostream> #include <cstring> #define N 100 using namespace std;struct time { int start; int end; };int c[N]; int temp=1;SELECT(time A[],int k,int n) {
2017-07-31 11:33:22
611
原创 收集苹果(动态规划法,二维)
//动态规划法 //平面上有x*y个格子,每个格子中放着一定数量的苹果。 //你从左上角的格子开始,每一步只能向下走或是向右走,每次走到一个格子上就把格子里的苹果收集起来, //这样下去,你最多能收集到多少个苹果。#include <iostream> #define N 100using namespace std; int MAX_APPLE(int A[][N], int x, int y)
2017-07-28 20:06:49
404
原创 最长非递减子序列LIS(动态规划法,一维)
//动态规划法 //LIS(时间复杂度为n平方)#include <iostream> #include <cstring> #define N 1000using namespace std;int LIS(int A[], int length) { int d[N]; for(int i=1;i<N;i++) d[i]=1; d[0]=0; for(int i=
2017-07-28 20:05:05
923
原创 最少硬币(动态规划法,初级)
//动态规划法 //硬币问题 //如果我们有面值为1元、3元和5元的硬币若干枚,如何用最少的硬币凑够11元? //(表面上这道题可以用贪心算法,但贪心算法无法保证可以求出解,比如1元换成2元的时候)//程序中v[]为硬币面值,下标从1开始,d[i]为i状态下的最优解#include <iostream> #include <cstring> #define N 1000 #define MAX 10
2017-07-28 20:03:08
683
原创 最长公共子序列LCS(动态规划法)
//动态规划法 //最长公共子序列 //c数组指出子序列长度,b数组为具体路径 #include <iostream> #define N 1000 #define UPLEFT 1 #define UP 2 #define LEFT 3int c[N][N],b[N][N];using namespace std;void PRINT_LCS(int b[][N],char X[],int i,i
2017-07-28 10:46:10
479
原创 钢条切割(动态规划法)
自顶向下递归方法 //自顶向下递归 //钢条切割 #include <iostream> #define N 1000 using namespace std;int CUT_ROD(int p[],int n) //p数组为价格,n为钢条长度 { int q=-1; if(n == 0) return 0; for(int i=1;i<=n;i++) {
2017-07-27 16:23:13
1028
原创 双链表的操作
#include <iostream> #define NIL 0 using namespace std;struct list { int data; list *last; list *next; list(int data) { this->data=data; this->last=NIL; this-
2017-07-25 15:22:47
207
原创 队列的数组实现
//队列的数组实现 //入队(ENQUEUE)时检查队列上溢,出队(DEQUEUE)时检查队列下溢 //head指向队头元素,tail指向队尾下一个新元素将要插入的位置 #include <iostream> #include <cstring> #include <stdlib.h> #include <stdio.h> #define N 1000using namespace std;int
2017-07-25 10:23:50
907
原创 栈的数组实现
//栈的数组实现 //数组从1开始,栈S在S[]处存放栈顶位置 #include <iostream> #include <cstring> #define N 1000using namespace std;int STACK_EMPTY(int S[]) { if(S[0]==0) return true; else return false; }void PUSH(int S[
2017-07-24 22:24:00
172
原创 寻找数组中的第i小元素
//选择数组中的第i小元素 #include <iostream> #include <cstring> #define N 1000 using namespace std;int search_max(int A[],int length) //数组从1开始 { int max=A[1]; for(int i=2;i<=length;i++) { if(m
2017-07-24 21:40:47
369
原创 最小值和最大值(3n/2)
//寻找n个元素中的最大值和最小值 //若n为奇数,最大值和最小值的初值设为第一个元素的值,若n为偶数,先对前两个元素进行比较,确定最大值和最小值 //对之后输入的元素两两比较然后再与最大值最小值比较,总比较次数最多为3n/2次 #include <iostream>using namespace std;int main(int argc, char *argv[]) { int n; c
2017-07-24 19:00:03
1449
原创 最小值和最大值(2n-2)
//寻找n个元素中的最大值和最小值 //普通方法进行寻找,进行2n-2次寻找#include <iostream>using namespace std;int main(int argc, char *argv[]) { //初始化 int n; cin >> n; int value; cin >>value; int max,min; max=m
2017-07-24 18:58:46
276
原创 桶排序
//桶排序 //在每个桶中使用插入排序 //即使数据不服从均匀分布,桶排序仍然可以在线性时间内完成 //线性时间完成条件:所有桶大小的平方和与总元素个数呈线性关系#include <iostream> #include <cstring> #define N 1000using namespace std;struct list //构造一个双向链表 { double data;
2017-07-24 11:25:39
210
原创 基数排序(基于计数排序)
//基于计数排序的基数排序 //输入中数组从1开始计数 #include <iostream> #include <cstring>const int N=1000;using namespace std;const int scale=10;int power(int digit) //位数幂运算 { if(digit==1) return 1; int value=1;
2017-07-20 17:05:13
353
原创 计数排序
不同于比较排序(插入排序、分治法合并排序,堆排序、快速排序),计数排序不需要进行数值的比较,但是处理过程中会占用较多的空间,不具有空间原址性。计数排序是稳定排序。//计数排序,时间复杂度为O(n) //输入中数组从1开始计数 //COUNTING_SORT中A[]为输入数组,B[]为输出数组,length为数组长度,k为输入数组中的最大值 #include <iostream> #include <
2017-07-20 17:04:23
162
原创 快速排序(分治法)
//快速排序 //PARTITION 函数对数组进行划分,选择A[end]值作为主元,start到i的值小于A[end],i+1到end-1的值大于A[end],最后将i+1和end处的值交换,实现数组的划分 #include <iostream>using namespace std;int PARTITION(int A[],int start,int end) { int mid_va
2017-07-19 11:24:25
618
原创 插入法建立一个最大堆
//用插入的方法建堆 #include <iostream> #define PARENT(i) i>>1 using namespace std;void MAX_HEAP_INSERT(int A[],int next) { while(1) { int par=PARENT(next); if(next>1 && A[par]<A[next])
2017-07-18 19:25:56
1612
原创 优先队列
//优先队列,基于最大堆实现最大优先队列,在数组中实现,其关键字(key)为数组元素 //数组元素下标为句柄(handle) //在最大堆和最大优先队列中数组下标都是从0开始,为了维持其父母孩子的相对位置关系#include <iostream> #define PARENT(i) i>>1; #define LEFT(i) i<<1; #define RIGHT(i) (i<<1)+1const
2017-07-18 18:54:42
155
原创 二分查找
//二分查找,在数组非递减地排序时对数组进行二分查找 #include <iostream>using namespace std;int Dich_search(int a[],int left,int right, int value) { if(left<right) { int mid=(left+right)/2; if(a[mid]==va
2017-07-18 16:26:28
153
原创 插入排序
//插入排序#include <iostream>const int N=10; using namespace std;int main(int argc, char *argv[]) { int a[N]; //输入 int n; cin >> n; for(int i=0;i<n;i++) { cin>>a[i];
2017-07-18 16:22:13
167
原创 分治法排序
//分治法排序#include <iostream> const int MAX=1000; using namespace std;void merge_sort(int A[],int p,int r); void merge(int A[],int p,int q,int r); int main(int argc, char *argv[]) {// int A[]={6,5,4,3,
2017-07-18 16:21:01
213
原创 最大连续子数组
输入一个数组,输出最大连续子数组区间,以及最大连续子数组和的值#include <iostream> #include <limits>using namespace std; const int MIN=1<<(sizeof(int)*8-1); // int MIN=numeric_limits<int>::min(); const int MAX=-(1<<(sizeof(int)*8
2017-07-18 16:19:16
218
原创 堆排序算法
//堆排序算法 #include <iostream> #define PARENT(i) i>>1; #define LEFT(i) i<<1; #define RIGHT(i) (i<<1)+1 using namespace std;// int PARENT(int i) {return i/2;} // int LEFT(int i) {return i*2;} // int RIGHT(
2017-07-18 16:01:08
172
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人