造反的孩子们 @流浪大叔的公园

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 
posted @ 2012-05-04 16:02 【造反的孩子们】@【流浪大叔的公园】 阅读(67) | 评论 (0) | 编辑 收藏
 
【一日一练】多线程 Sleep 方法的实现
 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 }
posted @ 2012-05-04 15:20 【造反的孩子们】@【流浪大叔的公园】 阅读(53) | 评论 (0) | 编辑 收藏
 

2012年5月2日

【每日一题】将键盘输入的英文字符转换为大写(IO包 InputStreameRedader,BufferedReader的运用)
 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 
posted @ 2012-05-02 01:57 【造反的孩子们】@【流浪大叔的公园】 阅读(124) | 评论 (0) | 编辑 收藏
 

2012年5月1日

【每日一题】文件复制(IO包 FileOutputStream的运用)
 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 
posted @ 2012-05-01 19:39 【造反的孩子们】@【流浪大叔的公园】 阅读(99) | 评论 (0) | 编辑 收藏
 

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 
posted @ 2012-04-30 23:15 【造反的孩子们】@【流浪大叔的公园】 阅读(51) | 评论 (0) | 编辑 收藏
 
【每日一题】遍历 某一文件夹下的所有 文件(夹)

 

 1/**//*
 2 * 编写一个程序,
 3 * 在命令行中以树状结构
 4 * 展现特定的文件夹机器子文件(夹)
 5 */

 6
 7import java.io.*;
 8
 9public class FileList {
10
11    public static void main(String[] args){
12        tree(new File("d:/Recycler/Legend_Wind"),0);
13    }

14    
15    private static void tree(File f,int level){
16        String preStr="";
17        for (int n = 0;n<level;n++) preStr +="\t\t";
18        
19        File[] sub=f.listFiles();
20        for(int i=0;i<sub.length;i++){
21            if(sub[i].isDirectory()){tree(sub[i],level+1);}
22            System.out.println(preStr+sub[i].getName());
23        }

24        System.out.println();
25    }

26}

27

 

posted @ 2012-04-30 13:00 【造反的孩子们】@【流浪大叔的公园】 阅读(75) | 评论 (0) | 编辑 收藏
 
【每日一题】解析 数字字符串 储存到二维数组
 1/**//*
 2 * 将一些日期散乱存入数组,
 3 * 然后用冒泡法升序排列
 4 * 并查找一个存在的日期,给出该日期所在数组中的位置
 5 */

 6public 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
60class 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}
posted @ 2012-04-30 12:59 【造反的孩子们】@【流浪大叔的公园】 阅读(102) | 评论 (0) | 编辑 收藏
 
【每日一题】日期存入数组,排序且查找并给出位置
 1/**//*
 2 * 将一些日期散乱存入数组,
 3 * 然后用冒泡法升序排列
 4 * 并查找一个存在的日期,给出该日期所在数组中的位置
 5 */

 6public 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
60class 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}
posted @ 2012-04-30 12:57 【造反的孩子们】@【流浪大叔的公园】 阅读(90) | 评论 (0) | 编辑 收藏
 
【每日一题】小孩围圈,数三退一

 

 1//假设500个小孩手拉着手围着一圈,数三就退出圈子,最后留在圈内的小孩是第几号?
 2
 3public class Count3Quit {
 4      public static void main(String[] args) {
 5              KidCircle kc = new KidCircle(500);
 6              
 7              Kid k = kc.first;
 8              int countNum = 0;
 9              while(kc.count > 1) {
10                   countNum ++;
11                   if(countNum == 3){
12                         countNum = 0;
13                           kc.del(k);  
14                    }

15                    k = k.right;
16               }

17               System.out.println("剩下的小孩编号是:"+(kc.first.id+1));
18       }
 
19}

20
21class Kid {
22      int id;
23      Kid left;
24      Kid right;
25}

26
27class KidCircle{
28       int count = 0;
29       Kid first,last;
30 
31
32       KidCircle (int n){
33        for (int i = 0;i<n;i++){add();}
34    }

35
36        void add(){
37            Kid k =new Kid();
38            k.id=count;
39            if (count<=0){
40                first=k;
41                last=k;
42                k.left=k.right=k;
43            }
 else {
44                k.left =last;
45                k.right =first;
46                last.right=first.left=last=k;
47            }

48            count++;
49        }

50
51        void del(Kid k){
52            if (count<=0){return;}
53            else if (count==1){first=last=null;}
54            else {
55                k.left.right=k.right;
56                k.right.left=k.left;
57                if (k==first){ first=k.right;}
58                else if (k==last){last=k.left;}
59            }

60            count--;
61        }

62}

 

posted @ 2012-04-30 12:56 【造反的孩子们】@【流浪大叔的公园】 阅读(86) | 评论 (0) | 编辑 收藏
 
【每日一题】JAVA:砝码问题
 1package 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
24public 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}
posted @ 2012-04-30 12:51 【造反的孩子们】@【流浪大叔的公园】 阅读(448) | 评论 (0) | 编辑 收藏
 
仅列出标题  
 
<2025年7月>
日一二三四五六
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

 导航

  • BlogJava
  • 首页
  • 发新随笔
  • 发新文章
  • 联系
  • 聚合
  • 管理

 统计

  • 随笔: 0
  • 文章: 10
  • 评论: 0
  • 引用: 0

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

文章档案(10)

  • 2012年5月 (4)
  • 2012年4月 (6)

搜索

  •  

最新评论


Powered by: 博客园
模板提供:沪江博客
Copyright ©2025 【造反的孩子们】@【流浪大叔的公园】