数据结构与算法
打鱼晒网
这个作者很懒,什么都没留下…
展开
-
hanoi算法递归非递归以及扩展
这是个汉诺塔程序,在调试的时候,输入的数字最好不要大于15,因为每大一个数所得的结果的步骤都会多一倍。如果你有耐心等待结果的话除外。汉诺塔是在欧洲流行的一种游戏,有a,b,c三个竿。a竿上有若干个由大到小的圆盘,大的在下面,小的在上面,b,c都是空杆,请你把a杆上的圆盘都倒到别的杆上,或b或c,在倒盘的过程中不可以大的压小的,实例程序如下:#include <stdio.h>...原创 2014-12-10 11:28:01 · 163 阅读 · 0 评论 -
c++实现双连表
#include<iostream>using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -2typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。typedef int ElemType; //ElemType ...原创 2014-12-15 09:17:15 · 217 阅读 · 0 评论 -
java对x的n尺方进行优化
public int power(int x,int n){ int y; if(n == 0){ y = 1; }else{ y = power(x, n/2); y = y*y; if(n%2 == 1){ y = y*x; } } return y;} 思想时利用递归方法,考虑n是奇数还是偶数情况是分别考虑,提高计算效率。...原创 2014-12-14 17:30:55 · 83 阅读 · 0 评论 -
各种排序总结
冒泡排序:public static void bubbleSort(int[] array){ int temp = 0; for(int i = 0; i < array.length; i++){ for(int j = 0; j < array.length - i - 1; j++){ if(array[j] > array[j+1]){...原创 2014-12-14 17:30:13 · 70 阅读 · 0 评论 -
折半查找
#include<iostream>using namespace std;#define MAXSIZE 100#define OK 1;typedef struct{ int key;//关键字域}ElemType;typedef struct{ ElemType *R; int length;}SSTable;int I...原创 2014-12-12 09:31:27 · 189 阅读 · 0 评论 -
java实现括号匹配
public boolean bracketMatch(String str){ Stack<Integer> s = new Stack<Integer>(); for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); switch(c){ case '{':...原创 2014-12-10 13:45:24 · 218 阅读 · 0 评论 -
java实现数制转换
public void baseConversion(int i){ Stack<String> s = new Stack<String>(); while(i > 0){ s.push(i%8+""); i=i/8; } while(!s.isEmpty()){ System.out.print(s.pop()); }...原创 2014-12-10 12:46:16 · 153 阅读 · 0 评论 -
合并两个数组为非递减数组
public int[] merge(int[] a,int[] b){ int pa = pb = pc = 0; int m = a.length; int n = b.length; int[] c = new int[m+n]; while(pa < m && pb < n){ if(a[pa] < b[pb]){ c...原创 2014-12-10 12:07:43 · 542 阅读 · 0 评论 -
求回文数涉及到的lcs算法实现
来源于:http://www.java3z.com/cwbwebhome/article/article18/report92.html?id=4867回文词是一种对称的字符串。任意给定一个字符串,通过插入若干字符,都可以变成回文词。现在的任务是,求出将给定字符串变成回文词所需要插入的最少字符数。比如:“Ab3bd”插入2个字符后可以变成回文词“dAb3bAd”或“Adb3bdA”,但...原创 2014-12-10 11:52:08 · 98 阅读 · 0 评论 -
最大公约数与最小公倍数
利用辗转相除法求最大公约数:#include <stdio.h>int hcf(int a,int b){ int r=0; while(b!=0) { r=a%b; a=b; b=r; } return(a);} int lcd(int u,int v,int h){ ...原创 2014-12-15 09:17:31 · 134 阅读 · 0 评论