2012年5月4日
1 2 public class TestPriority { 3 public static void main(String[] args) { 4 Thread t1 = new Thread(new T1()); 5 Thread t2 = new Thread(new T2()); 6 t1.setPriority(Thread.NORM_PRIORITY +3); //優先級設定 7 t1.start(); t2.start(); 8 } 9 10 } 11 12 class T1 implements Runnable { 13 public void run() { 14 for(int i=0;i<1000;i++){System.out.println("T1:"+i);} 15 } 16 } 17 18 class T2 implements Runnable { 19 public void run() { 20 for(int i=0;i<1000;i++){System.out.println("------------T2:"+i);} 21 } 22 } 23
1 import java.util.*; 2 3 public class TestInterrupt { 4 public static void main(String[] args){ 5 MyThread thread =new MyThread(); 6 thread.run(); 7 try{Thread.sleep(10000);} 8 catch (InterruptedException e){} 9 thread.interrupt(); 10 } 11 } 12 13 class MyThread extends Thread{ 14 boolean flag =true; 15 public void run() { 16 while(flag){ 17 System.out.println("==="+new Date()+"==="); 18 try { 19 sleep(1000); 20 } 21 catch (InterruptedException e) { 22 return; 23 } 24 } 25 } 26 27 }
2012年5月2日
1 import java.io.*; 2 3 public class TestTransForm { 4 5 public static void main(String[] args){ 6 InputStreamReader isr=new InputStreamReader(System.in); 7 BufferedReader br=new BufferedReader(isr); 8 String s=null; 9 10 try{ 11 s=br.readLine(); 12 while(s!=null){ 13 if(s.equalsIgnoreCase("exit"))break; 14 System.out.println(s.toUpperCase()); 15 s=br.readLine(); 16 } 17 br.close(); 18 19 } catch (IOException e){e.printStackTrace();} 20 } 21 } 22
2012年5月1日
1 import java.io.*; 2 3 public class TestFileOutputStream { 4 public static void main(String[] args){ 5 int b=0; 6 FileInputStream in=null; 7 FileOutputStream out=null; 8 9 try{ 10 in=new FileInputStream("c:/windows/notepad.exe"); 11 out=new FileOutputStream("c:/notepad.exe"); 12 while((b=in.read())!=-1){out.write(b);} 13 in.close(); 14 out.close(); 15 16 } catch (FileNotFoundException e2){ 17 System.out.println("找不到指定文件");System.exit(-1); 18 } catch (IOException e1){ 19 System.out.println("文件復制錯誤");System.exit(-1); 20 } 21 22 System.out.println("文件已復制"); 23 } 24 } 25
2012年4月30日
1 import java.util.*; 2 3 public class TestArgsWords { 4 5 public static void main(String args[]) { 6 Map<String, Integer> m = new HashMap<String, Integer>(); 7 8 for (int i = 0; i < args.length; i++) { 9 int freq = (Integer) m.get(args[i]) == null ? 0 : (Integer) m.get(args[i]); 10 m.put(args[i], freq==0 ? 1 : freq + 1); 11 } 12 13 System.out.println(m.size() + " distinct words detected:\n"+m); 14 } 15 } 16
1 /**//* 2 * 將一些日期散亂存入數組, 3 * 然后用冒泡法升序排列 4 * 并查找一個存在的日期,給出該日期所在數組中的位置 5 */ 6 public class TestDateSort { 7 public static void main(String[] args) { 8 Date[] days = new Date[5]; 9 days[0] = new Date(2006, 5, 4); 10 days[1] = new Date(2006, 7, 4); 11 days[2] = new Date(2008, 5, 4); 12 days[3] = new Date(2004, 5, 9); 13 days[4] = new Date(2004, 5, 4); 14 15 Date d = new Date(2006, 7, 4); 16 17 bubbleSort(days); 18 19 for(int i=0; i<days.length; i++) { 20 System.out.println(days[i]); 21 } 22 23 System.out.println(binarySearch(days, d)); 24 } 25 26 public static Date[] bubbleSort(Date[] a) { 27 int len = a.length; 28 for(int i = len-1;i>=1;i--) { 29 for(int j = 0;j<=i-1;j++) { 30 if(a[j].compare(a[j+1]) > 0) { 31 Date temp = a[j]; 32 a[j]=a[j+1]; 33 a[j+1]=temp; 34 } 35 } 36 } 37 return a; 38 } 39 40 public static int binarySearch(Date[] days, Date d) { 41 if (days.length==0) return -1; 42 43 int startPos = 0; 44 int endPos = days.length-1; 45 int m = (startPos + endPos) / 2; 46 while(startPos <= endPos) { 47 if(d.compare(days[m]) == 0) return m; 48 if(d.compare(days[m]) > 0) { 49 startPos = m + 1; 50 } 51 if(d.compare(days[m]) < 0) { 52 endPos = m -1; 53 } 54 m = (startPos + endPos) / 2; 55 } 56 return -1; 57 } 58 } 59 60 class Date { 61 int year, month, day; 62 63 Date(int y, int m, int d) { 64 year = y; month = m; day = d; 65 } 66 67 public int compare(Date date) { 68 return year > date.year ? 1 69 : year < date.year ? -1 70 : month > date.month ? 1 71 : month < date.month ? -1 72 : day > date.day ? 1 73 : day < date.day ? -1 : 0; 74 } 75 76 public String toString() { 77 return "Year:Month:Day -- " + year + "-" + month + "-" + day; 78 } 79 }
1 /**//* 2 * 將一些日期散亂存入數組, 3 * 然后用冒泡法升序排列 4 * 并查找一個存在的日期,給出該日期所在數組中的位置 5 */ 6 public class TestDateSort { 7 public static void main(String[] args) { 8 Date[] days = new Date[5]; 9 days[0] = new Date(2006, 5, 4); 10 days[1] = new Date(2006, 7, 4); 11 days[2] = new Date(2008, 5, 4); 12 days[3] = new Date(2004, 5, 9); 13 days[4] = new Date(2004, 5, 4); 14 15 Date d = new Date(2006, 7, 4); 16 17 bubbleSort(days); 18 19 for(int i=0; i<days.length; i++) { 20 System.out.println(days[i]); 21 } 22 23 System.out.println(binarySearch(days, d)); 24 } 25 26 public static Date[] bubbleSort(Date[] a) { 27 int len = a.length; 28 for(int i = len-1;i>=1;i--) { 29 for(int j = 0;j<=i-1;j++) { 30 if(a[j].compare(a[j+1]) > 0) { 31 Date temp = a[j]; 32 a[j]=a[j+1]; 33 a[j+1]=temp; 34 } 35 } 36 } 37 return a; 38 } 39 40 public static int binarySearch(Date[] days, Date d) { 41 if (days.length==0) return -1; 42 43 int startPos = 0; 44 int endPos = days.length-1; 45 int m = (startPos + endPos) / 2; 46 while(startPos <= endPos) { 47 if(d.compare(days[m]) == 0) return m; 48 if(d.compare(days[m]) > 0) { 49 startPos = m + 1; 50 } 51 if(d.compare(days[m]) < 0) { 52 endPos = m -1; 53 } 54 m = (startPos + endPos) / 2; 55 } 56 return -1; 57 } 58 } 59 60 class Date { 61 int year, month, day; 62 63 Date(int y, int m, int d) { 64 year = y; month = m; day = d; 65 } 66 67 public int compare(Date date) { 68 return year > date.year ? 1 69 : year < date.year ? -1 70 : month > date.month ? 1 71 : month < date.month ? -1 72 : day > date.day ? 1 73 : day < date.day ? -1 : 0; 74 } 75 76 public String toString() { 77 return "Year:Month:Day -- " + year + "-" + month + "-" + day; 78 } 79 }
1 package poise; 2 3 /** *//** 4 * 用天平稱重時,我們希望用盡可能少的砝碼組合稱出盡可能多的重量。 5 如果只有5個砝碼,重量分別是1,3,9,27,81。則它們可以組合稱出1到121之間任意整數重量(砝碼允許放在左右兩個盤中)。 6 本題目要求編程實現:對用戶給定的重量,給出砝碼組合方案。 7 例如: 8 用戶輸入: 9 5 10 程序輸出: 11 9-3-1 12 用戶輸入: 13 19 14 程序輸出: 15 27-9+1 16 輸入: 17 41 18 輸出: 19 81-27-9-3-1 20 要求程序輸出的組合總是大數在前小數在后。 21 可以假設用戶的輸入的數字符合范圍1~121。 22 **/ 23 24 public class Weight { 25 26 public static void main(String[]args) { 27 int input = 106; 28 int[] a , b , c , d , e; 29 a = b = c = d = e = new int[] {-1,0,1}; 30 for (int ai : a) { 31 for (int bi : b) { 32 for (int ci : c) { 33 for (int di : d) { 34 for (int ei : e) { 35 if (input == ei*81 + di*27 + ci*9 + bi*3 + ai*1) { 36 System.out.println("("+ ei*81 + ")+(" + di*27 + ")+(" + ci*9 + ")+(" + bi*3 + ")+(" + ai*1 +")"); 37 } 38 } 39 } 40 } 41 } 42 } 43 } 44 }
|
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 |
|
導航
統計
留言簿
文章檔案(10)
搜索
最新評論

|
|