代码改变世界

随笔分类 -  数据结构与算法

查找----散列查找

2012-08-25 11:19 by javaspring, 394 阅读, 收藏,
摘要: 1、散列函数 把任意长的输入消息串变化成固定长的输出串的一种函数。这个输出串称为该消息的杂凑值。一般用于产生消息摘要,密钥加密等。常见的散列函数构造方法如下:(1)直接定址法 例如:有一个从1到100岁的人口数字统计表,其中,年龄作为关键字,哈希函数取关键字自身。(2)数字分析法 有学生的生日数据如下: 年.月.日 75.10.03 75.11.23 76.03.02 76.07.12 75.04.21 76.02.15 ... 经分析,第一位,第二位,第三位重复的可能性大,取这三位造成冲突的机会增加,所以尽量不取前三位,取后三位比较好。(3)平方取中法... 阅读全文

查找----二分查找法

2012-08-25 10:59 by javaspring, 578 阅读, 收藏,
摘要: 1、二分查找法 二分查找法有一个很重要的前提条件:即待查找的序列必须是已经排好序的。 假设元素序列是按升序排列,将序列中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将序列分成前、后两个子序列,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子序列,否则进一步查找后一子序列。重复以上过程,直到找到满足条件的记录,查找成功,返回元素在序列中的索引,或直到子序列不存在为止,此时查找失败,返回-1。代码示例如下:int find2(int *array,int n,int val) { if (n<=0) { return -1; } int... 阅读全文

Java使用递归遍历文件夹

2012-07-29 22:31 by javaspring, 233 阅读, 收藏,
摘要: import java.io.*;class FileTest{ public static int cc=0; public static void Sereach(File f) { if(f.isFile()) { String str=f.getName().substring(f.getName().length()-4); if(str.equals(".bmp")||str.equals(".jpg")||str.equals(".BMP")||str.equals(".JPG")) { f.dele 阅读全文

2012-07-28 15:01 by javaspring, 294 阅读, 收藏,
摘要: //十进制数值M转换成m进制 void SysConvert(int N,int m) { while(N!=0) { push(p,N%m); //将余数进栈 N=N/m; } }1、栈的基本操作// lb.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdlib.h" #include <iostream> #include <string> using namespace std; // 阅读全文

链表

2012-07-28 11:42 by javaspring, 357 阅读, 收藏,
摘要: 1、链表基本操作----(带头结点)链表创建、增加节点、删除节点、链表反转// lb.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdlib.h" #include <iostream> #include <string> using namespace std; //定义链表的数据结构 template<class T> struct List{ T data; List *n 阅读全文

Java用LinkedList实现队列

2012-07-27 17:16 by javaspring, 401 阅读, 收藏,
摘要: import java.util.LinkedList; public class MyQueue { private LinkedList ll=new LinkedList(); public void put(Object o) { ll.addLast(o); } public Object get() { if(ll.isEmpty()) { System.out.println("队列为空,不能出队列了"); return null; } return ll.removeFirst(); } public boolean empt... 阅读全文

Java用LinkedList实现栈

2012-07-27 17:14 by javaspring, 250 阅读, 收藏,
摘要: import java.util.LinkedList; public class MyStack { private LinkedList ll=new LinkedList(); public void push(Object o) { ll.addFirst(o); } public Object pop() { if(ll.isEmpty()) { System.out.println("栈为空,不能出栈!"); return null; } return ll.removeFirst(); } public Object peek()... 阅读全文

Java跳出外层循环

2012-07-13 22:47 by javaspring, 719 阅读, 收藏,
摘要: 在这里介绍两种方法1.使用java的标号,结合break下面是代码public static void main(String[] args) { tiao:for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { System.out.println(i+"||"+j); if(i==2&&j==2) break tiao; } } }这里要注意的就是标号只能在for while do...while三中循环语句前定义,标签与循环语句之间不能有任何语句2.使得内部的循环条件不满足,外部循环条件也不满足... 阅读全文

杨辉三角

2012-07-07 12:52 by javaspring, 129 阅读, 收藏,
摘要: public class yanghui { public static void main(String []args) { int [][]b=new int[10][]; for(int i=0;i<10;i++) b[i]=new int[i+1]; for(int i=0;i<10;i++) for(int j=0;j<=i;j++) { if(j==0||j==i) { b[i][j]=1; continue; } else { b[i][j]=b[i-1][j-1]+b[i-1][j]; } } for(int m=0;m<1... 阅读全文

一道笔试题的搞笑解法

2012-06-26 22:38 by javaspring, 180 阅读, 收藏,
摘要: 原题是设计一个程序,求出712的729次方的最后4位数是多少?对于这道题目,我们可以规规矩矩的用一般方法解决,但更搞笑的是还有另种方法。我先贴出普通方法的代码#include <iostream.h> void main() { int result=1; for(int i=0;i<729;i++) { result=result*712; if(result>=10000) result=result%10000; } cout<<result<<endl; } 但更搞笑的是下面的Java代码... 阅读全文

求一个给定的数对应二进制数里有多少个1

2012-05-18 19:06 by javaspring, 1077 阅读, 收藏,
摘要: public class Test { public static void main(String[] args) { int count=0;int m=17; while(m!=0) { m=m&(m-1); count++; } System.out.println(count); }}这里面有一个就是求与运算,0&0=0;1&0=0;1&1=1;一个数减一之后,,再与自己求与的话结果只会减少一个一,这就是本算法的精华。 阅读全文

冒泡排序详细讲解

2012-04-20 22:56 by javaspring, 401 阅读, 收藏,
摘要: 冒泡排序是一种比较简单而且效率比较高的算法,冒泡排序最好的情况下的时间复杂度为O(n),也就是数据已经排好了,在最坏情况下,时间复杂度为O(n*n),也就是数据全都按关键字逆序排列。冒泡排序有两重循环,外层循环决定循环的次数,如果有n个数需要排序的话,那就要循环n-1次,这是因为n个数据只需要将最大的n-1个数排到最后就行了,里面的循环是进行比较,前面的数与后面的数进行比较,如果前面的数大于后面的数,就进行交换,外面的循环每进行一次,里面的循环就少进行一次。最初,里面的循环进行n-1次。代码如下#include <iostream.h> void main() { int a[10 阅读全文