├── src ├── antGame │ ├── Action.java │ ├── Position.java │ ├── AntGame.java │ ├── AntMap.java │ └── Ant.java ├── PrintInt.java ├── recursion │ ├── Recursion.java │ └── RecursiveTest.java ├── fibonacci │ └── Fibonacci.java ├── keyWordSubString │ ├── Comparator.java │ ├── ElementOperationInList.java │ ├── TestElementOperationInList.java │ └── Function.java ├── sort │ ├── Convention.java │ ├── BubbleSort.java │ └── CollectionsSortTest.java ├── prefixSorting │ ├── StackOperation.java │ ├── TestStackOperation.java │ └── TestPrefixSorting.java ├── printStar │ └── PrintStar.java ├── protocolTester │ └── ProtocolTester.java ├── segment │ ├── TestCoutArea.java │ ├── Function.java │ ├── CountArea.java │ └── TransTool.java ├── keyWordSubString2nd │ ├── ElementOperationInList.java │ └── Function.java ├── map │ ├── MapTest1.java │ ├── MapTest2.java │ ├── MapTest3.java │ └── Person.java ├── hanoi │ ├── Hanoi.java │ └── Hanoi2Colors.java ├── Sum1s.java ├── rankMultiplies │ └── RankMultiplies.java ├── pascal │ └── Pascal.java ├── moveBinary │ └── Function.java ├── KMP │ ├── KMP.java │ └── MyKMP.java ├── packInCycle │ ├── MyModel.java │ ├── PackInCycle.java │ └── CopyOfPackInCycle.java ├── quickSort │ └── QuickSort.java ├── testQuestion │ └── TestQuestion.java ├── CycleNFromS.java ├── knapsack │ └── Knapsack.java ├── stringMatch │ └── StringMatch.java ├── numFill │ └── NumFill.java ├── mouse │ └── Mouse.java └── InsertSort.java ├── .gitattributes └── .gitignore /src/antGame/Action.java: -------------------------------------------------------------------------------- 1 | package antGame; 2 | 3 | public enum Action { 4 | FORWARD,BACKWARD,UP,DOWN 5 | } 6 | -------------------------------------------------------------------------------- /src/antGame/Position.java: -------------------------------------------------------------------------------- 1 | package antGame; 2 | 3 | public class Position 4 | { 5 | int row; 6 | int col; 7 | } 8 | -------------------------------------------------------------------------------- /src/PrintInt.java: -------------------------------------------------------------------------------- 1 | 2 | public class PrintInt { 3 | 4 | public static void main(String[] args) 5 | { 6 | int i = 0; 7 | System.out.println(i+"0"); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/recursion/Recursion.java: -------------------------------------------------------------------------------- 1 | package recursion; 2 | /** 3 | *递归算法,求n的阶乘 4 | */ 5 | 6 | public class Recursion 7 | { 8 | int result=1; 9 | public int nRecursion(int n) 10 | { 11 | if (n > 0) 12 | { 13 | result = result * n; 14 | nRecursion(n-1); 15 | } 16 | return result; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/antGame/AntGame.java: -------------------------------------------------------------------------------- 1 | package antGame; 2 | 3 | public class AntGame 4 | { 5 | public static void main(String[] args) 6 | { 7 | int m = 6; 8 | AntMap antMap = new AntMap(m); 9 | Ant ant = new Ant(antMap); 10 | Position curPos = new Position(); 11 | int count = 1; 12 | while(count<=m*m) 13 | { 14 | ant.action(curPos, count++); 15 | } 16 | 17 | antMap.print(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/fibonacci/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package fibonacci; 2 | 3 | public class Fibonacci 4 | { 5 | public static void main(String[] args) 6 | { 7 | int[] fib = new int[20]; 8 | 9 | fib[0] = 0; 10 | fib[1] = 1; 11 | 12 | for(int i= 2;i dataStack , int depth) 8 | { 9 | int max = 0; 10 | Stack dataStackCopy = (Stack)dataStack.clone(); 11 | for(int i = 0;i < depth;i++) 12 | { 13 | max = dataStackCopy.peek()>max?dataStackCopy.peek():max; 14 | dataStackCopy.pop(); 15 | } 16 | return max; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/printStar/PrintStar.java: -------------------------------------------------------------------------------- 1 | package printStar; 2 | 3 | public class PrintStar 4 | { 5 | 6 | public static void printStar(int m,int max) 7 | { 8 | if(m>max) 9 | return; 10 | 11 | for(int i=1;i<=m;i++) 12 | System.out.print("*"); 13 | System.out.println(); 14 | printStar(m+1,max); 15 | 16 | for(int j=m-1;j>=1;j--) 17 | System.out.print("*"); 18 | System.out.println(); 19 | } 20 | 21 | public static void main(String[] args) 22 | { 23 | printStar(1,5); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/antGame/AntMap.java: -------------------------------------------------------------------------------- 1 | package antGame; 2 | 3 | public class AntMap 4 | { 5 | private int size; 6 | int[][] matrix; 7 | 8 | AntMap(int size) 9 | { 10 | this.size = size; 11 | matrix = new int[size][size]; 12 | } 13 | 14 | public int getSize() 15 | { 16 | return this.size; 17 | } 18 | 19 | void print() 20 | { 21 | for(int i=0;i values[j + 1]) 16 | { 17 | temp = values[j]; 18 | values[j] = values[j + 1]; 19 | values[j + 1] = temp; 20 | } 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /src/sort/CollectionsSortTest.java: -------------------------------------------------------------------------------- 1 | package sort; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | 6 | public class CollectionsSortTest 7 | { 8 | public static void main(String[] args) 9 | { 10 | // TODO Auto-generated method stub 11 | 12 | Integer[] num = {5,8,3,9,1}; 13 | //double[] num = {4.4,2.9,6.3}; //不可以 14 | //Double[] num = {4.4,2.9,6.3}; //可以 15 | Arrays.sort(num,Collections.reverseOrder()); //对整形数组按降序排序 16 | for(int i=0;i doubleList = new ArrayList(); 13 | doubleList.add(1.1); 14 | doubleList.add(2.1); 15 | doubleList.add(1.4); 16 | doubleList.add(3.1); 17 | doubleList.add(1.6); 18 | doubleList.add(2.0); 19 | doubleList.add(2.2); 20 | doubleList.add(3.0); 21 | doubleList.add(1.2); 22 | 23 | CountArea countArea = new CountArea(); 24 | ArrayList data = countArea.sortList(doubleList); 25 | System.out.println(data); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/prefixSorting/TestStackOperation.java: -------------------------------------------------------------------------------- 1 | package prefixSorting; 2 | 3 | import java.util.Stack; 4 | 5 | import org.junit.Test; 6 | import static org.junit.Assert.*; 7 | 8 | public class TestStackOperation { 9 | 10 | @Test 11 | public void testGetMaxNumFromStack() { 12 | Stack dataStack = new Stack(); 13 | dataStack.push(2); 14 | dataStack.push(34); 15 | dataStack.push(56); 16 | dataStack.push(12); 17 | dataStack.push(7); 18 | dataStack.push(23); 19 | dataStack.push(45); 20 | dataStack.push(32); 21 | dataStack.push(9); 22 | 23 | StackOperation stackOperation = new StackOperation(); 24 | int res = stackOperation.getMaxNumFromStack(dataStack, 9); 25 | System.out.println(dataStack.pop()); 26 | assertEquals(56,res); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/keyWordSubString/ElementOperationInList.java: -------------------------------------------------------------------------------- 1 | package keyWordSubString; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ElementOperationInList 6 | { 7 | public int getElementFromList(String s) 8 | { 9 | sourceTemp = s.split("/"); 10 | int index = 0; 11 | for(;index < targetList.size();index++) 12 | { 13 | targetTemp = targetList.get(index).split("/"); 14 | if(sourceTemp[0].endsWith(targetTemp[0])) 15 | return index; 16 | } 17 | return -1; 18 | } 19 | 20 | public ArrayList getTargetList() { 21 | return targetList; 22 | } 23 | 24 | public void setTargetList(ArrayList targetList) { 25 | this.targetList = targetList; 26 | } 27 | 28 | private String[] targetTemp; 29 | private String[] sourceTemp; 30 | private ArrayList targetList; 31 | } 32 | -------------------------------------------------------------------------------- /src/keyWordSubString2nd/ElementOperationInList.java: -------------------------------------------------------------------------------- 1 | package keyWordSubString2nd; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ElementOperationInList 6 | { 7 | public int getElementFromList(String s) 8 | { 9 | sourceTemp = s.split("/"); 10 | int index = 0; 11 | for(;index < targetList.size();index++) 12 | { 13 | targetTemp = targetList.get(index).split("/"); 14 | if(sourceTemp[0].endsWith(targetTemp[0])) 15 | return index; 16 | } 17 | return -1; 18 | } 19 | 20 | public ArrayList getTargetList() { 21 | return targetList; 22 | } 23 | 24 | public void setTargetList(ArrayList targetList) { 25 | this.targetList = targetList; 26 | } 27 | 28 | private String[] targetTemp; 29 | private String[] sourceTemp; 30 | private ArrayList targetList; 31 | } 32 | -------------------------------------------------------------------------------- /src/keyWordSubString/TestElementOperationInList.java: -------------------------------------------------------------------------------- 1 | package keyWordSubString; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.ArrayList; 6 | 7 | import org.junit.Test; 8 | 9 | public class TestElementOperationInList { 10 | 11 | @Test 12 | public void testGetElementFromList() { 13 | ElementOperationInList elementOperationInList = new ElementOperationInList(); 14 | ArrayList targetList = new ArrayList(); 15 | targetList.add("ww/0"); 16 | targetList.add("xx/2"); 17 | targetList.add("sd/4"); 18 | targetList.add("ss/6"); 19 | targetList.add("hh/8"); 20 | elementOperationInList.setTargetList(targetList); 21 | int res = elementOperationInList.getElementFromList("hd/15"); 22 | System.out.println("res = " + res); 23 | assertEquals(-1,res); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/map/MapTest1.java: -------------------------------------------------------------------------------- 1 | package map; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Map.Entry; 6 | 7 | //id相同的map映射,最后一次map键值将覆盖前几次的键值 8 | public class MapTest1 9 | { 10 | public static void main(String[] args) 11 | { 12 | Map m = new HashMap(); 13 | Person p1 = new Person(); 14 | Person p2 = new Person(); 15 | 16 | p1.setId("1"); 17 | p1.setName("name1"); 18 | p2.setId("1"); 19 | p2.setName("name2"); 20 | 21 | m.put(p1, "person1"); 22 | m.put(p2, "person2"); 23 | 24 | System.out.println("Map m's size :" + m.size()); 25 | 26 | for(Object o :m.entrySet()) 27 | { 28 | Entry e = (Entry)o; 29 | System.out.println("key:"+ e.getKey()); 30 | System.out.println("value:"+ e.getValue()); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/hanoi/Hanoi.java: -------------------------------------------------------------------------------- 1 | package hanoi; 2 | import java.io.BufferedReader; 3 | import java.io.IOException; 4 | import java.io.InputStreamReader; 5 | 6 | 7 | public class Hanoi 8 | { 9 | public static void main(String[] args) throws IOException 10 | { 11 | int n; 12 | BufferedReader buf; 13 | buf = new BufferedReader(new InputStreamReader(System.in)); 14 | 15 | System.out.print("請輸入盤數:"); 16 | n = Integer.parseInt(buf.readLine()); 17 | 18 | Hanoi hanoi = new Hanoi(); 19 | hanoi.move(n, 'A', 'B', 'C'); 20 | } 21 | 22 | public void move(int n, char a, char b, char c) 23 | { 24 | if(n == 1) 25 | System.out.println("盤 " + n + " 由 " + a + " 移至 " + c); 26 | else { 27 | move(n - 1, a, c, b); 28 | System.out.println("盤 " + n + " 由 " + a + " 移至 " + c); 29 | move(n - 1, b, a, c); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/map/MapTest2.java: -------------------------------------------------------------------------------- 1 | package map; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Map.Entry; 6 | 7 | public class MapTest2 8 | { 9 | public static void main(String[] args) 10 | { 11 | Map m = new HashMap(); 12 | Person p1 = new Person(); 13 | Person p2 = new Person(); 14 | 15 | p1.setId("1"); 16 | p1.setName("name1"); 17 | p2.setId("2"); //注意这里设置了不同的ID 18 | p2.setName("name2"); 19 | 20 | m.put(p1, "person1"); 21 | m.put(p2, "person2"); 22 | 23 | System.out.println("Map m's size :" + m.size()); 24 | 25 | p2.setId("1"); 26 | 27 | System.out.println("Map m's size :" + m.size()); 28 | 29 | for(Object o :m.entrySet()) 30 | { 31 | Entry e = (Entry)o; 32 | System.out.println("key:"+ e.getKey()); 33 | System.out.println("value:"+ e.getValue()); 34 | } 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Sum1s.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | public class Sum1s 5 | { 6 | 7 | 8 | public static void main(String[] args) 9 | { 10 | Scanner keyboard = new Scanner(System.in); 11 | int data = keyboard.nextInt(); 12 | int result = sum1s(data); 13 | System.out.println("result = " + result); 14 | } 15 | 16 | public static int sum1s(int n) 17 | { 18 | int iCount = 0; 19 | int iFactor = 1; 20 | 21 | int iLowerNum = 0; 22 | int iCurrNum = 0; 23 | int iHigherNum = 0; 24 | 25 | while(n/iFactor != 0) 26 | { 27 | iLowerNum = n - (n/iFactor)*iFactor; 28 | iCurrNum = (n/iFactor)%10; 29 | iHigherNum = n / (iFactor*10); 30 | 31 | switch (iCurrNum) 32 | { 33 | case 0: 34 | iCount += iHigherNum * iFactor; 35 | break; 36 | case 1: 37 | iCount += iHigherNum * iFactor + iLowerNum + 1; 38 | break; 39 | default: 40 | iCount += (iHigherNum + 1)*iFactor; 41 | break; 42 | } 43 | 44 | iFactor *= 10; 45 | } 46 | return iCount; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/rankMultiplies/RankMultiplies.java: -------------------------------------------------------------------------------- 1 | package rankMultiplies; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class RankMultiplies 8 | { 9 | public static int getRankMultiplies(int num) 10 | { 11 | if(num==1) 12 | return 1; 13 | else 14 | return num*getRankMultiplies(num-1); 15 | } 16 | 17 | public static int getResult(int end) 18 | { 19 | int result = 0; 20 | for(int index = 1;index<=end;index++) 21 | { 22 | result += getRankMultiplies(index); 23 | } 24 | 25 | return result; 26 | } 27 | 28 | public static void main(String[] args) 29 | { 30 | String inputData = ""; 31 | try { 32 | System.out.print("请输入最大阶乘数:"); 33 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 34 | inputData = in.readLine(); 35 | } catch (IOException e) { 36 | System.out.println("IOException:" + e.getMessage()); 37 | } 38 | 39 | System.out.println(); 40 | System.out.println("得到结果为:" + getResult(Integer.parseInt(inputData))); 41 | } 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/pascal/Pascal.java: -------------------------------------------------------------------------------- 1 | package pascal; 2 | 3 | import java.awt.Color; 4 | import java.awt.Graphics; 5 | 6 | import javax.swing.JFrame; 7 | 8 | public class Pascal extends JFrame 9 | { 10 | public Pascal() 11 | { 12 | setBackground(Color.white); 13 | setTitle("巴斯卡三角形"); 14 | setSize(520, 350); 15 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 | show(); 17 | } 18 | 19 | private long combi(int n, int r) 20 | { 21 | int i; 22 | long p = 1; 23 | 24 | for(i = 1; i <= r; i++) 25 | p = p * (n-i+1) / i; 26 | 27 | System.out.println("p=" + p + " n=" + n); 28 | return p; 29 | } 30 | 31 | public void paint(Graphics g) 32 | { 33 | final int N = 12; 34 | int n, r, t; 35 | 36 | for(n = 0; n <= N; n++) 37 | { 38 | for(r = 0; r <= n; r++) 39 | g.drawString(" " + combi(n, r), 40 | (N-n)*20 + r * 40, n * 20 + 50); 41 | } 42 | } 43 | 44 | public static void main(String[] args) 45 | { 46 | Pascal frm = new Pascal(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/map/MapTest3.java: -------------------------------------------------------------------------------- 1 | package map; 2 | /* 3 | * 此处的变化是将p1,p2的id设成不同,然后都作为key插入map,因为两个key不相同,所以我们的预测是都可以插入, 4 | * 此时map的size应该为2,待插入后我们修改p2的id为1,即与p1相同,这样就造成了两个entry的key相同的情况, 5 | * 测试再查看map的结构,看看是不是还是刚才插入的两项。 6 | * 此时我们不知道HashMap的内部实现,所以我们不知道它的实例会不会在数据插入后还继续维持key的唯一性。 7 | * 我们可以猜测的是三种答案: 8 | * 1.抛出异常,不允许修改p2的id与p1相同,维护key的唯一性; 9 | * 2.可以修改,但根据某种算法删除p1或p2中的一项,也能起到维护key的唯一性; 10 | * 3.可以修改,没有任何事情发生....两项id相同的person实例并存于map中,即存在同一个key对应了两个value。 11 | * */ 12 | 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | import java.util.Map.Entry; 16 | 17 | public class MapTest3 18 | { 19 | public static void main(String[] args) 20 | { 21 | Map m = new HashMap(); 22 | Person p1 = new Person(); 23 | Person p2 = new Person(); 24 | 25 | p1.setId("1"); 26 | p1.setName("name1"); 27 | p2.setId("2"); 28 | p2.setName("name2"); 29 | 30 | m.put(p1, "person1"); 31 | m.put(p2, "person2"); 32 | 33 | System.out.println("Map m's size :" + m.size()); 34 | 35 | p2.setId("1"); 36 | 37 | System.out.println("Map m's size :" + m.size()); 38 | 39 | for(Object o :m.entrySet()) 40 | { 41 | Entry e = (Entry)o; 42 | System.out.println("key:"+ e.getKey()); 43 | System.out.println("value:"+ e.getValue()); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/moveBinary/Function.java: -------------------------------------------------------------------------------- 1 | package moveBinary; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.TreeSet; 7 | 8 | public class Function 9 | { 10 | public static void main(String[] args) 11 | { 12 | System.out.println("请输入n位二进制数"); 13 | String inputData = ""; 14 | try { 15 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 16 | inputData = in.readLine(); 17 | } catch (IOException e) { 18 | // TODO Auto-generated catch block 19 | e.printStackTrace(); 20 | } 21 | System.out.println("-----------------------------------"); 22 | int size = inputData.length(); 23 | inputData += inputData; 24 | System.out.println("得到"+size+"位数字,输出"+size+"位偏移数如下:"); 25 | TreeSet dataTree = new TreeSet(); 26 | for(int index = 0;index0 && B[j]!=B[i]) 15 | { 16 | j = P[j]; 17 | } 18 | if(B[j] == B[i]) 19 | { 20 | j++; 21 | } 22 | P[i] = j; 23 | } 24 | return P; 25 | } 26 | 27 | public static void kmp(String parStr,String subStr) 28 | { 29 | int subSize = subStr.length(); 30 | int parSize = parStr.length(); 31 | char[] B = subStr.toCharArray(); 32 | char[] A = parStr.toCharArray(); 33 | int[] P = preProcess(B); 34 | System.out.println("P 的列表如下:"); 35 | for(int single:P) 36 | { 37 | System.out.println("single = " + single); 38 | } 39 | System.out.println("//////////////////////////////////////////"); 40 | int j=0; 41 | int k=0; 42 | for(int i=0;i0&&B[j]!=A[i]) 45 | { 46 | j = P[j-1]; 47 | } 48 | if(B[j] == A[i]) 49 | { 50 | j++; 51 | } 52 | if(j == subSize) 53 | { 54 | j = P[j-1]; 55 | k++; 56 | System.out.printf("Find subString '%s' at %d\n",subStr,i-subSize+1); 57 | } 58 | } 59 | System.out.printf("Totally found %d times for '%s'.\n", k,subStr); 60 | } 61 | 62 | public static void main(String[] args) 63 | { 64 | kmp("asdfhkasdsafabcabcdefasdfhasdf", "abcabcdef"); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/KMP/MyKMP.java: -------------------------------------------------------------------------------- 1 | package KMP; 2 | 3 | public class MyKMP 4 | { 5 | public static int[] preProcess(char[] B) 6 | { 7 | int size = B.length; 8 | int[] P = new int[size]; 9 | P[0] = 0; 10 | int j = 0; 11 | 12 | for(int i=1;i0 && B[j]!=B[i]) 15 | { 16 | j = P[j]; 17 | } 18 | if(B[j] == B[i]) 19 | { 20 | j++; 21 | } 22 | P[i] = j; 23 | } 24 | return P; 25 | } 26 | 27 | public static void kmp(String parStr,String subStr) 28 | { 29 | int subSize = subStr.length(); 30 | int parSize = parStr.length(); 31 | char[] B = subStr.toCharArray(); 32 | char[] A = parStr.toCharArray(); 33 | int[] P = preProcess(B); 34 | System.out.println("P 的列表如下:"); 35 | for(int single:P) 36 | { 37 | System.out.println("single = " + single); 38 | } 39 | System.out.println("//////////////////////////////////////////"); 40 | int j=0; 41 | int k=0; 42 | for(int i=0;i0&&B[j]!=A[i]) 45 | { 46 | j = P[j-1]; 47 | } 48 | if(B[j] == A[i]) 49 | { 50 | j++; 51 | } 52 | if(j == subSize) 53 | { 54 | j = P[j-1]; 55 | k++; 56 | System.out.printf("Find subString '%s' at %d\n",subStr,i-subSize+1); 57 | } 58 | } 59 | System.out.printf("Totally found %d times for '%s'.\n", k,subStr); 60 | } 61 | 62 | public static void main(String[] args) 63 | { 64 | kmp("asdfhkasdsafabcabcdefasdfhasdf", "abcabcdef"); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/map/Person.java: -------------------------------------------------------------------------------- 1 | package map; 2 | 3 | public class Person { 4 | 5 | /** 6 | * 身份id 7 | */ 8 | private String id; 9 | 10 | /** 11 | * 姓名 12 | */ 13 | private String name; 14 | 15 | public String getId() 16 | { 17 | return id; 18 | } 19 | 20 | public void setId(String id) 21 | { 22 | this.id = id; 23 | } 24 | 25 | public String getName() 26 | { 27 | return name; 28 | } 29 | 30 | public void setName(String name) 31 | { 32 | this.name = name; 33 | } 34 | 35 | @Override 36 | public int hashCode() 37 | { 38 | final int prime = 31; 39 | int result = 1; 40 | result = prime * result + ((id == null) ? 0 : id.hashCode()); 41 | return result; 42 | } 43 | 44 | @Override 45 | public boolean equals(Object obj) 46 | { 47 | if (this == obj) 48 | return true; 49 | if (obj == null) 50 | return false; 51 | if (getClass() != obj.getClass()) 52 | return false; 53 | Person other = (Person) obj; 54 | if (id == null) 55 | { 56 | if (other.id != null) 57 | return false; 58 | } else 59 | if (!id.equals(other.id)) 60 | return false; 61 | return true; 62 | } 63 | 64 | @Override 65 | public String toString() { 66 | return "Person [id=" + id + ", name=" + name + "]"; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/packInCycle/MyModel.java: -------------------------------------------------------------------------------- 1 | package packInCycle; 2 | 3 | import javax.swing.event.TableModelListener; 4 | import javax.swing.table.TableModel; 5 | 6 | public class MyModel implements TableModel 7 | { 8 | public MyModel(int num) 9 | { 10 | //创建一个二维数组,用于存放数据 11 | this.data = new String[num][num]; 12 | } 13 | 14 | //取得数组的方法 15 | public String[][] GetData() 16 | { 17 | return data; 18 | } 19 | 20 | public void addTableModelListener(TableModelListener l) 21 | { 22 | } 23 | 24 | public Class getColumnClass(int columnIndex) 25 | { 26 | //返回的数据类型 27 | return String.class; 28 | } 29 | 30 | public int getColumnCount() 31 | { 32 | return data[0].length; 33 | } 34 | 35 | public String getColumnName(int columnIndex) 36 | { 37 | return "第" + columnIndex + "列"; 38 | } 39 | 40 | public int getRowCount() 41 | { 42 | return data.length; 43 | } 44 | 45 | public Object getValueAt(int rowIndex, int columnIndex) 46 | { 47 | String str = data[rowIndex][columnIndex]; 48 | return str; 49 | } 50 | 51 | public boolean isCellEditable(int rowIndex, int columnIndex) 52 | { 53 | return false; 54 | } 55 | 56 | public void removeTableModelListener(TableModelListener l) 57 | { 58 | } 59 | 60 | public void setValueAt(Object value, int rowIndex, int columnIndex) 61 | { 62 | data[rowIndex][columnIndex]=(String)value; 63 | } 64 | 65 | private String[][] data; 66 | } 67 | -------------------------------------------------------------------------------- /src/recursion/RecursiveTest.java: -------------------------------------------------------------------------------- 1 | package recursion; 2 | 3 | public class RecursiveTest { 4 | /** 5 | * 递归实现 6 | * 7 | * @param n 8 | * @return 9 | */ 10 | public static double recursive(long n) { 11 | if (n == 1) { 12 | return Math.log(1); 13 | } else { 14 | return Math.log(n) + recursive(n - 1); 15 | } 16 | } 17 | 18 | /** 19 | * 非递归实现 20 | * 21 | * @param n 22 | * @return 23 | */ 24 | public static double directly(long n) { 25 | double result = 0; 26 | for (int i = 1; i <= n; i++) { 27 | result += Math.log(i); 28 | } 29 | return result; 30 | } 31 | 32 | public static void main(String[] args) { 33 | int i = 5000000; 34 | long test = System.nanoTime(); 35 | long start1 = System.nanoTime(); 36 | double r1 = recursive(i); 37 | long end1 = System.nanoTime(); 38 | long start2 = System.nanoTime(); 39 | double r2 = directly(i); 40 | long end2 = System.nanoTime(); 41 | 42 | System.out.println("recursive result:" + r1); 43 | System.out.println("recursive time used:" + (end1 - start1)); 44 | System.out.println("non-recursive result:" + r2); 45 | System.out.println("non-recursive time used:" + (end2 - start2)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/quickSort/QuickSort.java: -------------------------------------------------------------------------------- 1 | package quickSort; 2 | 3 | public class QuickSort { 4 | private static > void sortFirstMiddleLast(T[] a,int first ,int mid ,int last) 5 | { 6 | order(a,first,mid); 7 | order(a,mid,last); 8 | order(a,first,mid); 9 | } 10 | 11 | private static > void order(T[] a,int i,int j) 12 | { 13 | if(a[i].compareTo(a[j])>0) 14 | { 15 | swap(a,i,j); 16 | } 17 | } 18 | 19 | private static void swap(Object[] array,int i,int j) 20 | { 21 | Object temp = array[i]; 22 | array[i] = array[j]; 23 | array[j] = temp; 24 | } 25 | 26 | private static > int partition(T[] a,int first,int last) 27 | { 28 | int mid = (first+last)/2; 29 | sortFirstMiddleLast(a,first,mid,last); 30 | 31 | swap(a,mid,last-1); 32 | int pivotIndex = last-1; 33 | T pivot = a[pivotIndex]; 34 | 35 | int indexFromLeft = first +1; 36 | int indexFromRight = last - 2; 37 | 38 | boolean done = false; 39 | while(!done) 40 | { 41 | while(a[indexFromLeft].compareTo(pivot) <0) 42 | indexFromLeft++; 43 | 44 | while(a[indexFromRight].compareTo(pivot) >0) 45 | indexFromRight--; 46 | 47 | assert a[indexFromLeft].compareTo(pivot) >= 0 && a[indexFromRight].compareTo(pivot) <=0; 48 | 49 | if(indexFromLeft < indexFromRight) 50 | { 51 | swap(a,indexFromLeft,indexFromRight); 52 | indexFromLeft++; 53 | indexFromRight--; 54 | }else{ 55 | done = true; 56 | } 57 | } 58 | 59 | return pivotIndex; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/segment/Function.java: -------------------------------------------------------------------------------- 1 | package segment; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | 8 | public class Function 9 | { 10 | public static void main(String[] args) 11 | { 12 | System.out.println("请输入多组线段参数,比如2/3,代表(2,3),中间用空格分开:"); 13 | String data = ""; 14 | try { 15 | System.out.println("请输入包含M个单词的英文描述,用空格隔开:"); 16 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 17 | data = in.readLine(); 18 | } catch (IOException e) { 19 | System.out.println("IOException:" + e.getMessage()); 20 | } 21 | String[] inputData = data.split(" "); 22 | ArrayList dataList = new ArrayList(); //得到原始数列dataList 23 | for(String obj:inputData) 24 | { 25 | dataList.add(obj); 26 | } 27 | CountArea countArea = new CountArea(); 28 | ArrayList doubleList = countArea.getArea(dataList); //得到一个从小到大的Double数列doubleArray 29 | ArrayList segList = countArea.getSegment(doubleList); //得到从小到大的各个分段 30 | // double[][] doubleArray = countArea.transToArray(dataList); //得到一个double型的二维数组,第一列是原始线段起始点,第二列是原始线段结束点 31 | 32 | TransTool transTool = new TransTool(); 33 | ArrayList resList = new ArrayList(); 34 | int inCount; 35 | for(String src:segList) 36 | { 37 | inCount = 0; 38 | for(String des:dataList) 39 | { 40 | if(transTool.inSegment(src, des) == 0) 41 | { 42 | inCount++; 43 | } 44 | } 45 | if(inCount >1){ 46 | resList.add(src); 47 | } 48 | } 49 | ArrayList res = transTool.mergeResult(resList); 50 | transTool.printRes(res); 51 | } 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/segment/CountArea.java: -------------------------------------------------------------------------------- 1 | package segment; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class CountArea 6 | { 7 | public ArrayList getArea(ArrayList dataList) 8 | { 9 | String[] temp; 10 | for(String obj:dataList) 11 | { 12 | temp = obj.split("/"); 13 | res.add(Double.parseDouble(temp[0])); 14 | res.add(Double.parseDouble(temp[1])); 15 | } 16 | res = this.sortList(res); 17 | return res; 18 | } 19 | 20 | //把数列从小到大排序 21 | public ArrayList sortList(ArrayList doubleList) 22 | { 23 | int length = doubleList.size(); 24 | for(int index = 0;index doubleList.get(j + 1)) 29 | { 30 | doubleList.add(j, doubleList.get(j + 1)); 31 | doubleList.remove(j+2); 32 | } 33 | } 34 | } 35 | return doubleList; 36 | } 37 | 38 | public double[][] transToArray(ArrayList dataList) 39 | { 40 | int size = dataList.size(); 41 | double[][] data = new double[size][2]; 42 | String[] temp; 43 | for(int i = 0;i < size;i++) 44 | { 45 | temp = dataList.get(i).split("/"); 46 | data[i][0] = Double.parseDouble(temp[0]); 47 | data[i][1] = Double.parseDouble(temp[1]); 48 | } 49 | return data; 50 | } 51 | 52 | public ArrayList getSegment(ArrayList doubleList) 53 | { 54 | ArrayList arrayList = new ArrayList(); 55 | int length = doubleList.size(); 56 | for(int i = 0;i < length-1;i++) 57 | { 58 | arrayList.add(doubleList.get(i)+"/"+doubleList.get(i+1)); 59 | } 60 | return arrayList; 61 | } 62 | 63 | private static ArrayList res = new ArrayList(); 64 | } 65 | -------------------------------------------------------------------------------- /src/testQuestion/TestQuestion.java: -------------------------------------------------------------------------------- 1 | package testQuestion; 2 | 3 | import java.util.Iterator; 4 | import java.util.TreeSet; 5 | 6 | public class TestQuestion 7 | { 8 | private String[] b = new String[]{"1", "2", "2", "3", "4", "5"}; 9 | private int n = b.length; 10 | private boolean[] visited = new boolean[n]; 11 | private int[][] a = new int[n][n]; 12 | private String result = ""; 13 | private TreeSet TreeSet = new TreeSet(); 14 | 15 | public static void main(String[] args) 16 | { 17 | new TestQuestion().start(); 18 | } 19 | 20 | private void start() 21 | { 22 | for (int i = 0; i < n; i++) 23 | { 24 | for (int j = 0; j < n; j++) 25 | { 26 | if (i == j) { 27 | a[i][j] = 0; 28 | } else { 29 | a[i][j] = 1; 30 | } 31 | } 32 | } 33 | a[3][5] = 0; 34 | a[5][3] = 0; 35 | for (int i = 0; i < n; i++) 36 | { 37 | this.depthFirstSearch(i); 38 | } 39 | 40 | Iterator it = TreeSet.iterator(); 41 | while (it.hasNext()) 42 | { 43 | String string = (String) it.next(); 44 | 45 | if (string.indexOf("4") != 2) 46 | { 47 | System.out.println(string); 48 | } 49 | } 50 | } 51 | 52 | private void depthFirstSearch(int startIndex) 53 | { 54 | visited[startIndex] = true; 55 | result = result + b[startIndex]; 56 | if (result.length() == n) 57 | { 58 | TreeSet.add(result); 59 | } 60 | 61 | for(int j = 0; j < n; j++) 62 | { 63 | if (a[startIndex][j] == 1 && visited[j] == false) 64 | { 65 | depthFirstSearch(j); 66 | } else { 67 | continue; 68 | } 69 | } 70 | 71 | result = result.substring(0, result.length() -1); 72 | visited[startIndex] = false; 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /src/CycleNFromS.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | 5 | public class CycleNFromS 6 | { 7 | public CycleNFromS(String from,int times) 8 | { 9 | this.N = times; 10 | this.S = from; 11 | } 12 | 13 | public static void main(String[] args) 14 | { 15 | System.out.println("请按先后顺序输入初始值以及循环次数,并以空格隔开"); 16 | Scanner scanner = new Scanner(System.in); 17 | String[] data = scanner.nextLine().split(" "); 18 | String from = data[0]; 19 | int times = Integer.parseInt(data[1]); 20 | 21 | CycleNFromS initObj = new CycleNFromS(from,times); 22 | finalResList = initObj.equipResult(S); 23 | for(String str:finalResList) 24 | { 25 | System.out.print(str + " "); 26 | } 27 | } 28 | 29 | public int getStringLength(String sourceStr) 30 | { 31 | return sourceStr.length(); 32 | } 33 | 34 | public int stringToInteger(String sourceStr) 35 | { 36 | return Integer.parseInt(sourceStr); 37 | } 38 | 39 | /** 40 | * @param sourceStr 41 | * @return 42 | */ 43 | public ArrayList equipResult(String sourceStr) 44 | { 45 | int length = sourceStr.length(); 46 | Integer indexNum = Integer.parseInt(sourceStr); 47 | ArrayList resList = new ArrayList(); 48 | Integer nextNum = 0; 49 | for(int i = 0; i < N;i++) 50 | { 51 | String preResult = ""; 52 | nextNum = indexNum + i; 53 | if(nextNum.toString().indexOf("4") >= 0) 54 | { 55 | N++; 56 | continue; 57 | }else{ 58 | for(int index = length-indexNum.toString().length();index >0;index--) 59 | { 60 | preResult += "0"; 61 | } 62 | resList.add(preResult + nextNum); 63 | } 64 | } 65 | 66 | return resList; 67 | } 68 | 69 | private static ArrayList finalResList; 70 | private static String S = "000003"; 71 | private static int N = 10; 72 | } 73 | -------------------------------------------------------------------------------- /src/segment/TransTool.java: -------------------------------------------------------------------------------- 1 | package segment; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class TransTool { 6 | public int inSegment(String src,String des) 7 | { 8 | String[] tempSrc = src.split("/"); 9 | String[] tempDes = des.split("/"); 10 | double src1 = Double.parseDouble(tempSrc[0]); 11 | double src2 = Double.parseDouble(tempSrc[1]); 12 | double des1 = Double.parseDouble(tempDes[0]); 13 | double des2 = Double.parseDouble(tempDes[1]); 14 | if(src1>=des1&&src2<=des2){ 15 | return 0; 16 | }else if(src1des2){ 17 | return 1; 18 | }else if(src1des1&&src2>des2){ 21 | return 3; 22 | } 23 | return -1; 24 | } 25 | 26 | public ArrayList mergeResult(ArrayList resList) 27 | { 28 | ArrayList res = new ArrayList(); 29 | int size = resList.size(); 30 | String[] preStr; 31 | String[] nextStr; 32 | double preStrStart; 33 | double preStrEnd; 34 | double nextStrStart; 35 | double nextStrEnd; 36 | 37 | res.add(resList.get(0)); 38 | for(int i = 1;i res) 59 | { 60 | System.out.println("------------------------------------\n得到结果:"); 61 | String[] temp; 62 | for(String obj:res) 63 | { 64 | temp = obj.split("/"); 65 | System.out.print("(" + temp[0] + "," + temp[1] + ") "); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/knapsack/Knapsack.java: -------------------------------------------------------------------------------- 1 | package knapsack; 2 | 3 | class Fruit 4 | { 5 | private String name; 6 | private int size; 7 | private int price; 8 | 9 | public Fruit(String name, int size, int price) 10 | { 11 | this.name = name; 12 | this.size = size; 13 | this.price = price; 14 | } 15 | 16 | public String getName() 17 | { 18 | return name; 19 | } 20 | 21 | public int getPrice() 22 | { 23 | return price; 24 | } 25 | 26 | public int getSize() 27 | { 28 | return size; 29 | } 30 | } 31 | 32 | public class Knapsack 33 | { 34 | public static void main(String[] args) 35 | { 36 | final int MAX = 8; 37 | final int MIN = 1; 38 | int[] item = new int[MAX+1]; 39 | int[] value = new int[MAX+1]; 40 | 41 | Fruit fruits[] = { 42 | new Fruit("李子", 4, 4500), 43 | new Fruit("蘋果", 5, 5700), 44 | new Fruit("橘子", 2, 2250), 45 | new Fruit("草莓", 1, 1100), 46 | new Fruit("甜瓜", 6, 6700)}; 47 | 48 | for(int i = 0; i < fruits.length; i++) 49 | { 50 | for(int s = fruits[i].getSize(); s <= MAX; s++) 51 | { 52 | int p = s - fruits[i].getSize(); 53 | int newvalue = value[p] + 54 | fruits[i].getPrice(); 55 | if(newvalue > value[s]) {// 找到階段最佳解 56 | value[s] = newvalue; 57 | item[s] = i; 58 | } 59 | } 60 | } 61 | 62 | System.out.println("物品\t價格"); 63 | for(int i = MAX; 64 | i >= MIN; 65 | i = i - fruits[item[i]].getSize()) { 66 | System.out.println(fruits[item[i]].getName()+ 67 | "\t" + fruits[item[i]].getPrice()); 68 | } 69 | 70 | System.out.println("合計\t" + value[MAX]); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/stringMatch/StringMatch.java: -------------------------------------------------------------------------------- 1 | package stringMatch; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class StringMatch { 8 | private int[] skip; 9 | private int p; 10 | private String str; 11 | private String key; 12 | 13 | public StringMatch(String key) { 14 | skip = new int[256]; 15 | this.key = key; 16 | 17 | for(int k = 0; k <= 255; k++) 18 | skip[k] = key.length(); 19 | for(int k = 0; k < key.length() - 1; k++) 20 | skip[key.charAt(k)] = key.length() - k - 1; 21 | } 22 | 23 | public void search(String str) { 24 | this.str = str; 25 | p = search(key.length()-1, str, key); 26 | } 27 | 28 | private int search(int p, String input, String key) { 29 | while(p < input.length()) { 30 | String tmp = input.substring( 31 | p-key.length()+1, p+1); 32 | 33 | if(tmp.equals(key)) // 比較兩字串是否相同 34 | return p-key.length()+1; 35 | p += skip[input.charAt(p)]; 36 | } 37 | 38 | return -1; 39 | } 40 | 41 | public boolean hasNext() { 42 | return (p != -1); 43 | } 44 | 45 | public String next() { 46 | String tmp = str.substring(p); 47 | p = search(p+key.length()+1, str, key); 48 | return tmp; 49 | } 50 | 51 | public static void main(String[] args) throws IOException { 52 | BufferedReader bufReader = 53 | new BufferedReader( 54 | new InputStreamReader(System.in)); 55 | 56 | System.out.print("請輸入字串:"); 57 | String str = bufReader.readLine(); 58 | System.out.print("請輸入搜尋關鍵字:"); 59 | String key = bufReader.readLine(); 60 | 61 | StringMatch strMatch = new StringMatch(key); 62 | strMatch.search(str); 63 | 64 | while(strMatch.hasNext()) { 65 | System.out.println(strMatch.next()); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/hanoi/Hanoi2Colors.java: -------------------------------------------------------------------------------- 1 | package hanoi; 2 | 3 | public class Hanoi2Colors { 4 | public static void help() { 5 | System.out.println("Usage: java Hanoi2Colors number_of_disks"); 6 | System.out.println("\t number_of_disks: must be a even number."); 7 | System.exit(0); 8 | } 9 | 10 | public static void main(String[] args) { 11 | int disks = 0; 12 | try { 13 | disks = Integer.parseInt(args[0]); 14 | } catch (Exception e) { 15 | help(); 16 | } 17 | if ((disks <= 0) || (disks % 2 != 0)) { 18 | help(); 19 | } 20 | hanoi2colors(disks); 21 | } 22 | 23 | public static void hanoi(int disks,char source, char temp, char target) { 24 | if (disks == 1) { 25 | System.out.println("move disk from " 26 | + source + " to " + target); 27 | System.out.println("move disk from " 28 | + source + " to " + target); 29 | } else { 30 | hanoi(disks-1, source, target, temp); 31 | hanoi(1, source, temp, target); 32 | hanoi(disks-1, temp, source, target); 33 | } 34 | } 35 | 36 | public static void hanoi2colors(int disks) { 37 | char source = 'A'; 38 | char temp = 'B'; 39 | char target = 'C'; 40 | for (int i = disks / 2; i > 1; i--) { 41 | hanoi(i-1, source, temp, target); 42 | System.out.println("move disk from " 43 | + source + " to " + temp); 44 | System.out.println("move disk from " 45 | + source + " to " + temp); 46 | hanoi(i-1, target, temp, source); 47 | System.out.println("move disk from " 48 | + temp + " to " + target); 49 | } 50 | System.out.println("move disk from " 51 | + source + " to " + temp); 52 | System.out.println("move disk from " 53 | + source + " to " + target); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/antGame/Ant.java: -------------------------------------------------------------------------------- 1 | package antGame; 2 | 3 | public class Ant 4 | { 5 | AntMap map = null; 6 | Action curAction = Action.DOWN; 7 | 8 | public Ant(AntMap map) 9 | { 10 | this.map = map; 11 | } 12 | 13 | void action(Position pos,int count) 14 | { 15 | map.matrix[pos.row][pos.col] = count; 16 | 17 | if(shouldTurn(pos)) 18 | { 19 | turn(); 20 | } 21 | 22 | if(curAction == Action.FORWARD){ 23 | forward(pos); 24 | }else if(curAction == Action.DOWN){ 25 | down(pos); 26 | }else if(curAction == Action.BACKWARD){ 27 | backward(pos); 28 | }else if(curAction == Action.UP){ 29 | up(pos); 30 | } 31 | } 32 | 33 | private boolean shouldTurn(Position pos) 34 | { 35 | int min = 0; 36 | int max = map.getSize() - 1; 37 | 38 | if(curAction == Action.FORWARD) 39 | { 40 | if(pos.col >= max){ 41 | return true; 42 | }else if(map.matrix[pos.row][pos.col + 1]>0){ 43 | return true; 44 | } 45 | }else if(curAction == Action.DOWN){ 46 | if(pos.row >= max){ 47 | return true; 48 | }else if(map.matrix[pos.row+1][pos.col]>0){ 49 | return true; 50 | } 51 | }else if(curAction == Action.BACKWARD){ 52 | if(pos.row <= min){ 53 | return true; 54 | }else if(map.matrix[pos.row - 1][pos.col] > 0){ 55 | return true; 56 | } 57 | } 58 | 59 | return false; 60 | } 61 | 62 | private void turn() 63 | { 64 | if(curAction == Action.FORWARD){ 65 | curAction = Action.UP; 66 | }else if(curAction == Action.DOWN){ 67 | curAction = Action.FORWARD; 68 | }else if(curAction == Action.BACKWARD){ 69 | curAction = Action.DOWN; 70 | }else if(curAction == Action.UP){ 71 | curAction = Action.BACKWARD; 72 | } 73 | } 74 | 75 | private void forward(Position pos) 76 | { 77 | if(pos != null){ 78 | pos.col++; 79 | } 80 | } 81 | 82 | private void backward(Position pos) 83 | { 84 | if(pos != null){ 85 | pos.col--; 86 | } 87 | } 88 | 89 | private void up(Position pos) 90 | { 91 | if(pos != null){ 92 | pos.row--; 93 | } 94 | } 95 | 96 | private void down(Position pos) 97 | { 98 | if(pos != null){ 99 | pos.row++; 100 | } 101 | } 102 | } 103 | 104 | 105 | -------------------------------------------------------------------------------- /src/numFill/NumFill.java: -------------------------------------------------------------------------------- 1 | package numFill; 2 | 3 | public class NumFill { 4 | 5 | public static int number = 4; 6 | public static int circleNum = (number+1)/2; 7 | 8 | public static int getCircle(int i, int j) 9 | { 10 | int c = 1; 11 | 12 | 13 | if(i>circleNum) i = number+1-i; 14 | if(j>circleNum) j = number+1-j; 15 | c= (i0;j--) 37 | { 38 | maxNum = stackOperation.getMaxNumFromStack(dataStack, j); 39 | maxIndex = dataCount - dataStack.indexOf(maxNum) - 1; //倒序排列 40 | 41 | System.out.println("-------------------------------------"); 42 | System.out.println("maxIndex = " + maxIndex); 43 | System.out.println("-------------------------------------"); 44 | 45 | for(int index = 0;index <= maxIndex;index++) 46 | { 47 | System.out.println(dataStack.peek() + " -> queueDataTemp"); 48 | queueDataTemp.offer(dataStack.pop()); 49 | } 50 | while(!queueDataTemp.isEmpty()) 51 | { 52 | System.out.println("queueDataTemp -> " + queueDataTemp.peek() + " -> dataStack"); 53 | dataStack.add(queueDataTemp.remove()); 54 | } 55 | for(int index = 0;index < j;index++) 56 | { 57 | System.out.println("dataStack -> " + dataStack.peek() + " -> queueDataTemp"); 58 | queueDataTemp.offer(dataStack.pop()); 59 | } 60 | while(!queueDataTemp.isEmpty()) 61 | { 62 | System.out.println("queueDataTemp -> " + queueDataTemp.peek() + " -> dataStack"); 63 | dataStack.add(queueDataTemp.remove()); 64 | } 65 | } 66 | 67 | System.out.println("-----------------------------------最终结果"); 68 | while(!dataStack.isEmpty()){ 69 | System.out.print(dataStack.pop() + " "); 70 | } 71 | } 72 | 73 | private static String inputData = ""; 74 | private static int dataCount = 0; 75 | private static int maxNum; 76 | private static int maxIndex; 77 | private static Queue queueDataTemp = new LinkedList(); 78 | private static StackOperation stackOperation = new StackOperation(); 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/mouse/Mouse.java: -------------------------------------------------------------------------------- 1 | package mouse; 2 | 3 | public class Mouse { 4 | private int startI, startJ; // 入口 5 | private int endI, endJ; // 出口 6 | private boolean success = false; 7 | 8 | public static void main(String[] args) { 9 | int[][] maze = {{2, 2, 2, 2, 2, 2, 2}, 10 | {2, 0, 0, 0, 0, 0, 2}, 11 | {2, 0, 2, 0, 2, 0, 2}, 12 | {2, 0, 0, 2, 0, 2, 2}, 13 | {2, 2, 0, 2, 0, 2, 2}, 14 | {2, 0, 0, 0, 0, 0, 2}, 15 | {2, 2, 2, 2, 2, 2, 2}}; 16 | 17 | System.out.println("顯示迷宮:"); 18 | for(int i = 0; i < maze.length; i++) { 19 | for(int j = 0; j < maze[0].length; j++) 20 | if(maze[i][j] == 2) 21 | System.out.print("█"); 22 | else 23 | System.out.print(" "); 24 | System.out.println(); 25 | } 26 | 27 | Mouse mouse = new Mouse(); 28 | mouse.setStart(1, 1); 29 | mouse.setEnd(5, 5); 30 | 31 | if(!mouse.go(maze)) { 32 | System.out.println("\n沒有找到出口!"); 33 | } 34 | else { 35 | System.out.println("\n找到出口!"); 36 | for(int i = 0; i < maze.length; i++) { 37 | for(int j = 0; j < maze[0].length; j++) { 38 | if(maze[i][j] == 2) 39 | System.out.print("█"); 40 | else if(maze[i][j] == 1) 41 | System.out.print("◇"); 42 | else 43 | System.out.print(" "); 44 | } 45 | System.out.println(); 46 | } 47 | } 48 | } 49 | 50 | public void setStart(int i, int j) { 51 | this.startI = i; 52 | this.startJ = j; 53 | } 54 | 55 | public void setEnd(int i, int j) { 56 | this.endI = i; 57 | this.endJ = j; 58 | } 59 | 60 | public boolean go(int[][] maze) { 61 | return visit(maze, startI, startJ); 62 | } 63 | 64 | private boolean visit(int[][] maze, int i, int j) { 65 | maze[i][j] = 1; 66 | 67 | if(i == endI && j == endJ) 68 | success = true; 69 | 70 | if(!success && maze[i][j+1] == 0) 71 | visit(maze, i, j+1); 72 | if(!success && maze[i+1][j] == 0) 73 | visit(maze, i+1, j); 74 | if(!success && maze[i][j-1] == 0) 75 | visit(maze, i, j-1); 76 | if(!success && maze[i-1][j] == 0) 77 | visit(maze, i-1, j); 78 | 79 | if(!success) 80 | maze[i][j] = 0; 81 | 82 | return success; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/keyWordSubString2nd/Function.java: -------------------------------------------------------------------------------- 1 | package keyWordSubString2nd; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | 8 | import keyWordSubString.ElementOperationInList; 9 | 10 | public class Function 11 | { 12 | public static void main(String[] args) 13 | { 14 | String loop = "N"; 15 | do{ 16 | try { 17 | System.out.println("请输入包含M个单词的英文描述,用空格隔开:"); 18 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 19 | inputData = in.readLine(); 20 | } catch (IOException e) { 21 | System.out.println("IOException:" + e.getMessage()); 22 | } 23 | 24 | System.out.println("-----------------------------------"); 25 | try { 26 | System.out.println("请输入包含N个单词的英文关键词,用空格隔开:"); 27 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 28 | inputKeyWords = in.readLine(); 29 | } catch (IOException e) { 30 | System.out.println("IOException:" + e.getMessage()); 31 | } 32 | 33 | String[] inputDataArray = inputData.split(" "); 34 | String[] inputKeyWordsArray = inputKeyWords.split(" "); 35 | int keyWordsLength = inputKeyWordsArray.length; 36 | int i = 0; 37 | for(String targetword:inputDataArray) 38 | { 39 | if(indexList.size() >= keyWordsLength){ 40 | break; 41 | }else{ 42 | for(String keyword:inputKeyWordsArray) 43 | { 44 | if(targetword.matches(keyword)){ 45 | if(indexList.isEmpty()){ 46 | indexList.add(inputDataArray[i]+ "/" + i); 47 | }else{ 48 | elementOperationInList.setTargetList(indexList); 49 | tempIndex = elementOperationInList.getElementFromList(inputDataArray[i]+"/"+i); 50 | 51 | if(tempIndex >= 0){ 52 | System.out.println("删除的元素为:" + indexList.remove(tempIndex)); 53 | indexList.add(inputDataArray[i] + "/" + i); 54 | }else{ 55 | indexList.add(inputDataArray[i] + "/" + i); 56 | } 57 | } 58 | } 59 | } 60 | } 61 | i++; 62 | } 63 | 64 | int startIndex = 0; 65 | int endIndex = 0; 66 | 67 | String[] startPoint = indexList.get(0).split("/"); 68 | String[] endPoint = indexList.get(keyWordsLength - 1).split("/"); 69 | startIndex = Integer.parseInt(startPoint[1]); 70 | endIndex = Integer.parseInt(endPoint[1]); 71 | 72 | String resultStr = ""; 73 | for(int index = startIndex;index <= endIndex;index++) 74 | { 75 | resultStr += inputDataArray[index] + " "; 76 | } 77 | 78 | System.out.println("---------------------------------------"); 79 | System.out.println("结果是:" + resultStr); 80 | 81 | System.out.println("---------------------------------------"); 82 | System.out.println("继续吗?(Y/N)"); 83 | try { 84 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 85 | loop = in.readLine(); 86 | } catch (IOException e) { 87 | System.out.println("IOException:" + e.getMessage()); 88 | } 89 | }while(loop.endsWith("Y")|| loop.endsWith("y")); 90 | System.out.println("游戏结束!"); 91 | } 92 | 93 | private static ArrayList indexList = new ArrayList(); 94 | private static ElementOperationInList elementOperationInList = new ElementOperationInList(); 95 | private static int tempIndex; 96 | private static String inputData = ""; 97 | private static String inputKeyWords = ""; 98 | } 99 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /src/InsertSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class InsertSort { 4 | /** 5 | * 插入排序算法实现 6 | * @author Jason 7 | * @param arr 8 | * @return 9 | */ 10 | public static int[] insertSort(int[] arr) { 11 | int searchCount = 0; 12 | for (int out = 1; out < arr.length; out++) { 13 | outPrint(arr); 14 | if (arr[out]>arr[out-1]) { 15 | continue; 16 | } 17 | //普通查找算法 18 | /*for (int inn = 0; inn < out; inn++) { 19 | searchCount++; 20 | if (arr[out] arr[searchIndex]) { 34 | start = searchIndex + 1; 35 | } else if (arr[out] < arr[searchIndex]) { 36 | if (searchIndex == 0 37 | || (searchIndex != 0 && arr[out] > arr[searchIndex - 1])) { 38 | move(arr, out, searchIndex); 39 | break; 40 | } else { 41 | end = searchIndex - 1; 42 | continue; 43 | } 44 | } else { 45 | move(arr, out, searchIndex); 46 | break; 47 | } 48 | } 49 | } 50 | check(arr); 51 | System.out.println("Search Count:" + searchCount); 52 | return arr; 53 | } 54 | /** 55 | * 移动数据 56 | */ 57 | private static void move(int[] arr, int out, int inn) { 58 | int changeTemp = arr[out]; 59 | for (int i = out; i > inn; i--) { 60 | arr[i] = arr[i - 1]; 61 | } 62 | arr[inn] = changeTemp; 63 | } 64 | /** 65 | * 输出 66 | */ 67 | private static void outPrint(int[] arr) { 68 | System.out.println(); 69 | for (int i = 0; i < arr.length; i++) { 70 | System.out.print(arr[i] + "|"); 71 | } 72 | } 73 | /** 74 | * 结果检查 75 | */ 76 | private static boolean check(int[] arr) { 77 | System.out.println(); 78 | for (int i = 1; i < arr.length; i++) { 79 | if (arr[i] < arr[i - 1]) { 80 | System.out.println("Sort Error!"); 81 | return false; 82 | } 83 | } 84 | System.out.println("Sort Success!"); 85 | return true; 86 | } 87 | /** 88 | * 测试方法 89 | * @param args 90 | */ 91 | public static void main(String[] args) { 92 | // TODO Auto-generated method stub 93 | Random ran = new Random(); 94 | int[] arr = new int[60]; 95 | for (int i = 0; i < arr.length; i++) { 96 | arr[i]=ran.nextInt(300000); 97 | } 98 | System.out.println("Befor sort:"); 99 | for (int i = 0; i < arr.length; i++) { 100 | System.out.print(arr[i]+"|"); 101 | } 102 | arr = InsertSort.insertSort(arr); 103 | System.out.println("After sort:"); 104 | for (int i = 0; i < arr.length; i++) { 105 | System.out.print(arr[i]+"|"); 106 | } 107 | 108 | } 109 | } 110 | 111 | -------------------------------------------------------------------------------- /src/keyWordSubString/Function.java: -------------------------------------------------------------------------------- 1 | package keyWordSubString; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | 8 | public class Function 9 | { 10 | public static void main(String[] args) 11 | { 12 | try { 13 | System.out.println("请输入包含M个单词的英文描述,用空格隔开:"); 14 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 15 | inputData = in.readLine(); 16 | } catch (IOException e) { 17 | // TODO Auto-generated catch block 18 | System.out.println("IOException:" + e.getMessage()); 19 | } 20 | 21 | System.out.println("-----------------------------------"); 22 | try { 23 | System.out.println("请输入包含N个单词的英文关键词,用空格隔开:"); 24 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 25 | inputKeyWords = in.readLine(); 26 | } catch (IOException e) { 27 | // TODO Auto-generated catch block 28 | System.out.println("IOException:" + e.getMessage()); 29 | } 30 | 31 | // System.out.println("-----------------------------------"); 32 | String[] inputDataArray = inputData.split(" "); 33 | String[] inputKeyWordsArray = inputKeyWords.split(" "); 34 | int dataLength = inputDataArray.length; 35 | int keyWordsLength = inputKeyWordsArray.length; 36 | // System.out.println("////////////////////////////////////////////////////"); 37 | // System.out.println("dataLength = " + dataLength + "; keyWordsLength = " + keyWordsLength); 38 | 39 | Comparator comparator = new Comparator(); 40 | comparator.setStr(inputKeyWordsArray); 41 | 42 | int startIndex = 0; 43 | int endIndex = 0; 44 | 45 | for(int index = 0;index= keyWordsLength){ 48 | break; 49 | }else if(comparator.equaledWith(inputDataArray[index])){ 50 | if(indexList.isEmpty()){ 51 | // System.out.println("inputDataArray[index] = " + inputDataArray[index]+"/"+index); 52 | indexList.add(inputDataArray[index]+ "/" + index); 53 | }else{ 54 | elementOperationInList.setTargetList(indexList); 55 | // System.out.println("inputDataArray[index] = " + inputDataArray[index]+"/"+index); 56 | tempIndex = elementOperationInList.getElementFromList(inputDataArray[index]+"/"+index); 57 | 58 | // System.out.println("/////////////////////////////////////////////"); 59 | // System.out.println("tempIndex = " + tempIndex); 60 | // for(String obj:indexList) 61 | // { 62 | // System.out.println(obj); 63 | // } 64 | // System.out.println("tempIndex = " + tempIndex); 65 | 66 | if(tempIndex > 1){ 67 | indexList.remove(tempIndex - 1); 68 | indexList.add(inputDataArray[index] + "/" + index); 69 | }else{ 70 | indexList.add(inputDataArray[index] + "/" + index); 71 | } 72 | } 73 | } 74 | } 75 | 76 | // System.out.println("/////////////////////////////////////////////"); 77 | // for(String obj:indexList) 78 | // { 79 | // System.out.println(obj); 80 | // } 81 | 82 | String[] startPoint = indexList.get(0).split("/"); 83 | String[] endPoint = indexList.get(keyWordsLength - 1).split("/"); 84 | startIndex = Integer.parseInt(startPoint[1]); 85 | endIndex = Integer.parseInt(endPoint[1]); 86 | 87 | String resultStr = ""; 88 | for(int index = startIndex;index <= endIndex;index++) 89 | { 90 | resultStr += inputDataArray[index] + " "; 91 | } 92 | 93 | System.out.println("---------------------------------------"); 94 | System.out.println("结果是:" + resultStr); 95 | } 96 | 97 | private static ArrayList indexList = new ArrayList(); 98 | private static int tempIndex; 99 | private static ElementOperationInList elementOperationInList = new ElementOperationInList(); 100 | private static int startIndex = 0; 101 | private static int endIndex = 0; 102 | private static String inputData = ""; 103 | private static String inputKeyWords = ""; 104 | } 105 | -------------------------------------------------------------------------------- /src/packInCycle/PackInCycle.java: -------------------------------------------------------------------------------- 1 | package packInCycle; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class PackInCycle 8 | { 9 | //创建一个主函数 10 | public static void main(String[] args) 11 | { 12 | try { 13 | System.out.print("请输入矩阵边长:N="); 14 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 15 | N = Integer.parseInt(in.readLine()); 16 | } catch (IOException e) { 17 | // TODO Auto-generated catch block 18 | System.out.println("IOException:" + e.getMessage()); 19 | } 20 | 21 | //创建一个登录对象 22 | PackInCycle lg = new PackInCycle(); 23 | //调用其中的方法 24 | try { 25 | lg.UI(); 26 | } catch (Exception e) { 27 | // TODO Auto-generated catch block 28 | System.out.println("Exception:" + e.getMessage()); 29 | } 30 | } 31 | 32 | public void UI() throws Exception 33 | { 34 | //先创建一个窗体 35 | javax.swing.JFrame jf = new javax.swing.JFrame("螺旋打印数组"); 36 | 37 | //设置窗体的大小 38 | jf.setSize(N*40, N*40); 39 | 40 | //创建一个流式布局管理器 41 | java.awt.FlowLayout fl = new java.awt.FlowLayout(); 42 | jf.setLayout(fl); 43 | 44 | //创建一个表格对象 45 | table = new javax.swing.JTable(); 46 | 47 | //创建一个表格模型对象 48 | model = new MyModel(N); 49 | 50 | //将表格模型加到表格对象中 51 | table.setModel(model); 52 | 53 | //设置表格的高度 54 | table.setRowHeight(30); 55 | 56 | //逐一设置宽度 57 | for(int col=0;col