编程珠玑上一道练习题解题方案

code format="java" package htjs.site.service.bo.gen.engine.util;

import java.io.*; import java.util.*; import java.util.Random;

public final class AlgoTest { public static void main(String[] args) {

Date start = new Date; //new AlgoTest.findTheLostNumber; /*String tt = "jkdjsohj"; char[] t = tt.toCharArray; swap(t); System.out.println(t);*/ /*  new AlgoTest.find; new AlgoTest.sign; new AlgoTest.sort; new AlgoTest.merge; */       // new AlgoTest.shark("abcdefghijklmnopqrstuvwxyz", 10); System.out.println((char )('0'-1)); new AlgoTest.readLargeFile; Date stop = new Date;

System.out.println("$$$$$$$$$$$共用时： " + (stop.getTime - start.getTime) + " 毫秒");

}

public void shark(String s, int k) { char[] cs = s.toCharArray; int len = cs.length; reverse(cs, 0, k - 1); reverse(cs, k, len - 1); reverse(cs, 0, len - 1);

System.out.println(cs);

}

public static void reverse(char[] c, int k, int s) { for (int i = k, j = s; i <= (k + s) / 2.0; i++, j--) { char temp = c[i]; c[i] = c[j]; c[j] = temp; }

}

public void readLargeFile { try { PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(                               new FileOutputStream("D:/dicMerge.txt"), "UTF-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace; //To change body of catch statement use File | } catch (FileNotFoundException e) { e.printStackTrace; //To change body of atchc statement use File | }           BufferedReader bin = new BufferedReader(                   new InputStreamReader(new FileInputStream(new File("D:/Data.txt")))); String str = bin.readLine; long index = 0; while ((str) != null) { try { index++; str = bin.readLine; } catch (Exception e) { System.out.println(index); }

}

out.flush; out.close; bin.close; } catch (IOException ioe) { }

}

public void merge { try { PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(                               new FileOutputStream("D:/dicMerge.txt"), "UTF-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace; //To change body of catch statement use File | } catch (FileNotFoundException e) { e.printStackTrace; //To change body of atchc statement use File | }           BufferedReader bin = new BufferedReader(new InputStreamReader( new FileInputStream(new File("D:/dicSort.txt")))); String str = ""; String temp = bin.readLine; List list = new ArrayList;

String m = temp.split("  ")[1]; while ((str = bin.readLine) != null) { if (temp.split("  ")[0].equals(str.split("   ")[0])) { m = m + "  " + str.split("   ")[1]; } else {

m = temp.split("  ")[0] + "   " + m;                    out.print(m + "\n"); temp = str; m = temp.split("  ")[1]; }

}

out.flush; out.close; bin.close; } catch (IOException ioe) { }

}

public void sort { try { PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(                               new FileOutputStream("D:/dicSort.txt"), "UTF-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace; //To change body of catch statement use File } catch (FileNotFoundException e) { e.printStackTrace; //To change body of atchc statement use File }           BufferedReader bin = new BufferedReader(					new InputStreamReader( new FileInputStream(new File("D:/dicSign.txt")))); String str = null; List list = new ArrayList; while ((str = bin.readLine) != null) {

list.add(new String(str)); //写你的处理代码， System.out.println(str);

}           ComparatorForMap cfm = new ComparatorForMap; Collections.sort(list, cfm); for (int i = 0; i < list.size; i++) { out.print(list.get(i) + "\n"); }

out.flush; out.close; bin.close; } catch (IOException ioe) { }

}

public void sign { try { PrintWriter out = null; try { out = new PrintWriter(new				BufferedWriter(new OutputStreamWriter(				new FileOutputStream("D:/dicSign.txt"), "UTF-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace; //To change body of catch statement use File } catch (FileNotFoundException e) { e.printStackTrace; //To change body of atchc statement use File }           BufferedReader bin = new BufferedReader(					new InputStreamReader(new FileInputStream(					new File("D:/dic.txt")))); String str = bin.readLine; while ((str) != null) { if (str.length < 3) { ;               } else { String temp = str; char[] tt = str.toCharArray; swap(tt); temp = new String(tt) + "  " + temp + "\n"; out.print(temp);

}

//写你的处理代码， System.out.println(str); str = bin.readLine; }

out.flush; out.close; bin.close; } catch (IOException ioe) { }   }

public static void swap(char[] tt) { int len = tt.length; for (int i = 0; i < len; i++) { char temp = tt[i]; char temp1 = tt[i]; int index = i;           for (int j = i + 1; j < len; j++) { if (temp > tt[j]) { index = j;                   temp = tt[j]; }           }            tt[i] = temp; tt[index] = temp1;

}       System.out.println(tt); }

public void find { try { PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(                               new FileOutputStream("D:/dic.txt"), "UTF-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace; //To change body of catch statement use File } catch (FileNotFoundException e) { e.printStackTrace; //To change body of atchc statement use File }           BufferedReader bin = new BufferedReader(new InputStreamReader( new FileInputStream(			new File("C:\\Users\\MJ\\AppData\\Local\\Temp\\牛津英汉词典.txt")))); String str = null; while ((str = bin.readLine) != null) { if (str.length < 3) { ;               } else if (str.charAt(0) == 'v' && str.charAt(1) == '.') ; else if (str.charAt(0) < 'A' || str.charAt(0) > 'z') ; else if (str.indexOf(".") < 6 && str.indexOf(".") >= 0) ; else if (str.charAt(0) >= 'A' && str.charAt(0) <= 'z'				&& str.charAt(1) >= 'A' && str.charAt(1) <= 'z') { String[] temp = str.split(" "); out.print(temp[0] + "\n"); }

//写你的处理代码， System.out.println(str);

}

out.flush; out.close; bin.close; } catch (IOException ioe) { }   }

public void findTheLostNumber { PrintWriter out = null; try { out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(new FileOutputStream("D:/Data.txt"), "UTF-8"))); } catch (UnsupportedEncodingException e) { e.printStackTrace; //To change body of catch statement use File } catch (FileNotFoundException e) { e.printStackTrace; //To change body of atchc statement use File }       Random ran = new Random; for (int i = 0; i < 10000; i++) { for (int j = 0; j < 100000; j++) { if (ran.nextInt(10000) > 9900) ; else { String temp = j + "";

for (int k = 0; k < 5 - temp.length; k++) temp = "0" + temp; out.print(i + temp + "\n"); }           }        }

out.flush; out.close; } }

final class ComparatorForMap implements Comparator {

public ComparatorForMap {

}

public int compare(Object arg0, Object arg1) { String s0 = (String) arg0; String s1 = (String) arg1; int index = 0; for (int i = 0; i < (s0.length < s1.length ? s0.length : s1.length); i++) { if (s0.charAt(i) == s1.charAt(i)) index++; else break; }       if (index == (s0.length < s1.length ? s0.length : s1.length)) return s0.length == s1.length ? 0 : (s0.length < s1.length ? -1 : 1); else { return s0.charAt(index) == s1.charAt(index) ? 0 : s0.charAt(index) > s1.charAt(index) ? 1 : -1;       }

} }

code