写着玩的练习1

1.冒泡

[java]  view plain copy
  1. static void sort(int[] arry)  
  2. {  
  3.   
  4.     for(int i=0;i<arry.length-1;i++)  
  5.     {         
  6.         //System.out.println("lentgh=>"+arry.length);      
  7.         if(arry[i]<arry[i+1])  
  8.         {  
  9.             int temp=0;  
  10.             //System.out.println("[i]=>"+arry[i]);  
  11.             //System.out.println("[i]+1=>"+arry[i+1]);  
  12.             temp=arry[i];  
  13.             arry[i]=arry[i+1];  
  14.             arry[i+1]=temp;  
  15.               
  16.             i=-1;//从新检查排序, 自加后归零  
  17.         }  
  18.           
  19.     }  
  20.   
  21. }  


2.按规定的个数,给字符串截取成若干组

[java]  view plain copy
  1. String test="123156498478645614651";  
  2. for(int i=0;i<test.length();i+=5)  
  3. {  
  4.     System.out.println(test.substring(i, ( i+5 >test.length() ? test.length() :i+5 )));  
  5. }  

3.生产者消费者

[java]  view plain copy
  1. public class Per_Cer {  
  2.       
  3.     public static void main(String[] args) {  
  4.           
  5.         CangKu ck=new CangKu(5);  
  6.         P p =new P(ck);  
  7.         C c =new C(ck);  
  8.           
  9.         Thread t1=new Thread(p);  
  10.         Thread t2=new Thread(c);  
  11.           
  12.         t1.start();  
  13.         t2.start();  
  14.     }  
  15.   
  16. }  
  17.   
  18. class CangKu  
  19. {  
  20.     public int num=0;  
  21.     boolean isEpt=true;  
  22.     static Object l=new Object();  
  23.       
  24.     public CangKu(int n)  
  25.     {  
  26.         this.num=n;  
  27.     }  
  28.       
  29.     public synchronized void put(int i)  
  30.     {  
  31.         //System.out.println(Thread.currentThread()+" put"+i);  
  32.         //synchronized(l)  
  33.         {  
  34.             if(!isEpt)  
  35.             {  
  36.                 try {  
  37.                     this.wait();  
  38.                 } catch (InterruptedException e) {  
  39.                     // TODO Auto-generated catch block  
  40.                     e.printStackTrace();  
  41.                 }  
  42.             }     
  43.           
  44.             num=i;  
  45.             isEpt=false;  
  46.             System.out.println(Thread.currentThread()+"=>put"+i);  
  47.               
  48.             notifyAll();  
  49.         }  
  50.     }  
  51.       
  52.     public synchronized int get()  
  53.     {  
  54.         //System.out.println(Thread.currentThread()+" get");  
  55.         //synchronized(l)  
  56.         {  
  57.             if(isEpt )  
  58.             {  
  59.                 try {  
  60.                     this.wait();  
  61.                 } catch (InterruptedException e) {  
  62.                     // TODO Auto-generated catch block  
  63.                     e.printStackTrace();  
  64.                 }  
  65.                   
  66.                   
  67.             }  
  68.   
  69.             isEpt=true;  
  70.             System.out.println("         "+Thread.currentThread()+"=>get"+num);  
  71.               
  72.             notifyAll();  
  73.             return num;  
  74.               
  75.               
  76.         }  
  77.     }  
  78.       
  79.       
  80. }  
  81.   
  82.   
  83. class P implements Runnable{  
  84.       
  85.     public CangKu ck=null;  
  86.       
  87.     public P(CangKu c)  
  88.     {  
  89.         this.ck = c;  
  90.     }  
  91.       
  92.   
  93.     @Override  
  94.     public void run() {  
  95.           
  96.         while(true)  
  97.         {//  
  98.             //System.out.println(" pppppppppp ");  
  99.             ck.put((int)(Math.random()*100));  
  100.             try {  
  101.                 Thread.sleep((int)(Math.random()*2000));  
  102.             } catch (InterruptedException e) {  
  103.                 // TODO Auto-generated catch block  
  104.                 e.printStackTrace();  
  105.             }  
  106.         }  
  107.           
  108.     }  
  109.       
  110.   
  111. }  
  112.   
  113.   
  114. class C implements Runnable{  
  115.       
  116.     public CangKu ck=null;  
  117.       
  118.     public C(CangKu c)  
  119.     {  
  120.         this.ck = c;  
  121.     }  
  122.       
  123.   
  124.     @Override  
  125.     public void run() {  
  126.           
  127.         while(true)  
  128.         {  
  129.             //System.out.println(" cccccccccccccc "+    );  
  130.             ck.get();  
  131.               
  132.             try {  
  133.                 Thread.sleep((int)(Math.random()*2000));  
  134.             } catch (InterruptedException e) {  
  135.                 // TODO Auto-generated catch block  
  136.                 e.printStackTrace();  
  137.             }  
  138.         }  
  139.           
  140.     }  
  141.       
  142.   
  143. }  



1:用单层for循环控制输出乘法表

		//单层for, 当行列数相等时换行
		for(int i=1,j=1; j<=9;i++)
		{
			System.out.print(" "+i*j+" ");
			if(i == j) //行列号相等时, 就该换行了
			{
				i=0;
				j++;
				System.out.println();
			}
		}


2.素数中用"开方" 缩小循环范围

	public static boolean isP(int i)
	{
		
		if(i==0 || i==1 )//一:2是素数,不用剔除出去
		{
			return false;
		}
		
		double iii = Math.sqrt(i);//二:取平方根Math.sqrt(i), 且应该是<= 比如i为4
		for(int j=2;j<=iii;j++) //三:写在for里边影响效率;j<=(Math.sqrt(i)); 写在外边更好      
		{
						
			if(i%j==0)
			{
				return false;
			}
			
		}
		
		return true;
		
	}


3.回文数 ( ABCCBA)

	public static boolean isH(int n)
	{
		if( n == revsNum(n) ) //原参数 是否等于反转后的参数
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public static int revsNum(int n)
	{
		int old = n;
		int tar = 0;
		
		while(old > 0) //这个比较有意思, 用整除10的方式筛选出最低位数字
		{
			tar *= 10; //相当于把上次筛选出来的数字左移
			tar += old % 10;//加上新筛选出的最低位
			
			old /= 10;//原数字可以降低位数了
		}
		
		//System.out.println("tar=>"+tar);
		return tar;	
	}


4.Date对象的操作

Date dd =new Date(d.getTime()  + 1000*60*60*24); //getTime返回计算机时间


5.

要求:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。

1. /**  
2.          *  编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。  
3.          *  但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,  
4.          *  应该输出为"我ABC"而不是"我ABC+汉的半个"。  
5.          */  
6.         public static String subString(String str,int len){   
7.             if(str == null && "".equals(str)){   
8.                 return null;   
9.             }   
10.             //将字符串中的char数组转换成指定编码方式的byte数组的函数   
11.             byte[] strBytes = null;   
12.             try {   
13.                 strBytes = str.getBytes("GBK");   
14.                    
15.             } catch (UnsupportedEncodingException e) {   
16.                 e.printStackTrace();   
17.             }   
18.             //得到字符串的长度,判断截取字符串的长度是否在判断的范围内,否则返回原串   
19.             int strLen = strBytes.length;   
20.             if(len >= strLen || len < 1){   
21.                 return str;   
22.             }   
23. //          System.out.println("strBytes.length="+strBytes.length);   
24. //          System.out.println("len="+len);   
25.             int count = 0;   
26.             for(int i=0; i<len; i++){   
27.                 //将每个字节数组转换为整型数,以为后面根据值的正负来判断是否为汉字   
28.                 int value = strBytes[i];   
29. //              System.out.print(value+",");   
30.                 //如果是汉字(负),则统计截取字符串中的汉字所占字节数   
31.                 if(value < 0){      
32.                     count++;   
33.                 }   
34. //              System.out.println("zh count="+count);   
35.             }   
36.             //依据判断给定的字符串是否含有汉字,利用String类的substring()方法来截取不同的长度   
37.                
38.             //根据所统计的字节数,判断截取到字符是否为半个汉字,奇数为半个汉字   
39.             if(count % 2 !=0){   
40.                 //如果在截取长度为1时,则将该汉字取出,   
41.                 //其他情况则不截取这里的截取长度则按字符长度截取(截取字节长度数-截取汉字字节数/2-截取到的半个汉字的字节数)   
42.                 len = (len == 1)?len:len-count/2-1;   
43. //              System.out.println("处理后的len="+len);   
44.                    
45.             }else{   
46.                 //截取字符长度为字节长度-汉字所占字节长度/2(汉字占两个字节)   
47.                 len = len-(count/2);   
48.             }   
49.                 return str.substring(0,len);   
50.            
51.         }   
52.         public static void main(String[] args) {   
53.             //情况一:   
54.             String inStr = "我ABC你";      
55.             String str = subString(inStr, 6);      
56.             System.out.println(str);   //我ABC   
57.                
58.           //情况二:首字符为汉字   
59.             inStr = "我ABC汉DEF";      
60.             str = subString(inStr, 1);      
61.             System.out.println(str);   //我   
62.                
63.           //情况三:中间有连续汉字   
64.            inStr = "我AB爱孩子CDEF";   
65.            str = subString(inStr,9);      
66.            System.out.println(str);   //我AB爱孩   
67.                
68.          //情况四:没有汉字   
69.            inStr = "ABCDEF";   
70.            str = subString(inStr,4);      
71.            System.out.println(str);   //ABCD   
72.         }  
 













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值