现在的位置: 首页 > 综合 > 正文

两道算法题

2013年02月03日 ⁄ 综合 ⁄ 共 3823字 ⁄ 字号 评论关闭

 

1. 字符串截取,输入为一个字符串和一个数字,但是如果遇到汉字不能只截取半个汉字。

 

Java代码 复制代码
  1. package cn.lifx.test;   
  2.   
  3. public class GetNBitChar    
  4. {   
  5.     public static void main(String[] args)   
  6.     {   
  7.         String str1 = "中abc国人def伟大";   
  8.            
  9.         GetNBitChar test = new GetNBitChar();   
  10.            
  11.         String str2 = test.getStr(str1, 1);   
  12.         System.out.println(str2);   
  13.            
  14.         str2 = test.getStr(str1, 4);   
  15.         System.out.println(str2);   
  16.            
  17.         str2 = test.getStr(str1, 6);   
  18.         System.out.println(str2);   
  19.            
  20.         str2 = test.getStr(str1, 8);   
  21.         System.out.println(str2);   
  22.            
  23.         str2 = test.getChineseChars(str1);   
  24.         System.out.println(str2);   
  25.     }   
  26.        
  27.     //获取前n个字节   
  28.     public String getStr(String str, int n)   
  29.     {   
  30.         String temp = "";   
  31.            
  32.         int count = 0;   
  33.         int index = 1;   
  34.            
  35.         while(count < n)   
  36.         {   
  37.             if(str.substring(index-1, index).getBytes().length == 1)   
  38.             {   
  39.                 count++;   
  40.             }   
  41.             else  
  42.             {   
  43.                 count = count + 2;   
  44.             }   
  45.                
  46.             index++;   
  47.         }   
  48.            
  49.         temp = str.substring(0, index-1);   
  50.            
  51.         return temp;   
  52.     }   
  53.        
  54.     //获得所有汉字   
  55.     public String getChineseChars(String str)   
  56.     {   
  57.         String temp = "";   
  58.         StringBuffer sb = new StringBuffer();   
  59.            
  60.         for(int i=1; i<=str.length(); i++)   
  61.         {   
  62.             temp = str.substring(i-1, i);   
  63.             if(temp.getBytes().length == 2)   
  64.             {   
  65.                 sb.append(temp);   
  66.             }   
  67.         }   
  68.            
  69.         return sb.toString();   
  70.     }   
  71. }  

 

 

2. 一个数组有2n个整数,其中有n个奇数,n个偶数,要求把奇数放在奇数位置,偶数放在偶数位置。

 

Java代码 复制代码
  1. package cn.lifx.test;   
  2.   
  3. public class SortOddAndEvenNum    
  4. {   
  5.     public static void main(String[] args)   
  6.     {   
  7.         int[] arr = {23468579};   
  8.            
  9.         SortOddAndEvenNum sort = new SortOddAndEvenNum();   
  10.            
  11.         sort.Display(arr, "Before:");   
  12.            
  13.         sort.Sort(arr);   
  14.            
  15.         sort.Display(arr, "After:");   
  16.     }   
  17.        
  18.     public void Sort(int[] arr)   
  19.     {   
  20.         int index1 = 0;   
  21.         int index2 = 1;   
  22.            
  23.         int temp = 0;   
  24.         int n = arr.length;   
  25.            
  26.         while(index1 < n && index2 < n)   
  27.         {   
  28.             while(arr[index1] % 2 != 0)   
  29.             {   
  30.                 index1 += 2;   
  31.             }   
  32.                
  33.             while(arr[index2] % 2 == 0)   
  34.             {   
  35.                 index2 += 2;   
  36.             }   
  37.                
  38.             temp = arr[index1];   
  39.             arr[index1] = arr[index2];   
  40.             arr[index2] = temp;   
  41.                
  42.             index1 += 2;   
  43.             index2 += 2;   
  44.         }   
  45.     }   
  46.        
  47.     public void Display(int[] arr, String str)   
  48.     {   
  49.         System.out.println(str);   
  50.         for(int i=0; i<arr.length; i++)   
  51.         {   
  52.             System.out.print(arr[i] + " ");   
  53.         }   
  54.         System.out.println();   
  55.     }   
  56. }  

抱歉!评论已关闭.