递归,排序等算法编程题

7、递归算法题1

一个整数,大于0,不用循环和本地变量,按照n,2n,4n,8n的顺序递增,当值大于5000时,把值按照指定顺序输出来。
例:n=1237
则输出为:
1237,
2474,
4948,
9896,
9896,
4948,
2474,
1237,

提示:写程序时,先致谢按递增方式的代码,写好递增的以后,再增加考虑递减部分。

   public static void doubleNum(int n)

   {

      System.out.println(n);

      if(n<=5000)

        doubleNum(n*2);

      System.out.println(n);  

   }

                       

  
  

Gaibaota(N) = Gaibaota(N-1) + n

   
  

  

7、递归算法题2

第1个人10,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?

  1. package cn.itcast;  
  2.   
  3.    
  4.   
  5. importjava.util.Date;  
  6.   
  7.    
  8.   
  9. public class A1 {  
  10.   
  11.    
  12.   
  13.    public static void main(String [] args)  
  14.   
  15.    {  
  16.   
  17.       System.out.println(computeAge(8));  
  18.   
  19.    }  
  20.   
  21.      
  22.   
  23.    public static int computeAge(int n)  
  24.   
  25.    {  
  26.   
  27.       if(n==1return 10;  
  28.   
  29.       return computeAge(n-1)+ 2;  
  30.   
  31.    }  
  32.   
  33. }  
  34.   
  35.    
  36.   
  37.    public static void toBinary(int n,StringBuffer result)  
  38.   
  39.    {  
  40.   
  41.    
  42.   
  43.       if(n/2 != 0)  
  44.   
  45.         toBinary(n/2,result);  
  46.   
  47.       result.append(n%2);     
  48.   
  49.    }  
package cn.itcast;

 

importjava.util.Date;

 

public class A1 {

 

   public static void main(String [] args)

   {

      System.out.println(computeAge(8));

   }

   

   public static int computeAge(int n)

   {

      if(n==1) return 10;

      return computeAge(n-1)+ 2;

   }

}

 

   public static void toBinary(int n,StringBuffer result)

   {

 

      if(n/2 != 0)

        toBinary(n/2,result);

      result.append(n%2);   

   }


 

94、排序都有哪几种方法?请列举。用JAVA实现一个快速排序。

本人只研究过冒泡排序、选择排序和快速排序,下面是快速排序的代码:

  1. public class QuickSort {  
  2. /** 
  3. * 快速排序 
  4. * @param strDate 
  5. * @param left 
  6. * @param right 
  7. */  
  8. public void quickSort(String[] strDate,int left,int right){  
  9. String middle,tempDate;  
  10. int i,j;  
  11. i=left;  
  12. j=right;  
  13. middle=strDate[(i+j)/2];  
  14. do{  
  15. while(strDate[i].compareTo(middle)<0&& i<right)  
  16. i++; //找出左边比中间值大的数   
  17. while(strDate[j].compareTo(middle)>0&& j>left)  
  18. j--; //找出右边比中间值小的数   
  19. if(i<=j){ //将左边大的数和右边小的数进行替换    
  20. tempDate=strDate[i];  
  21. strDate[i]=strDate[j];  
  22. strDate[j]=tempDate;  
  23. i++;  
  24. j--;  
  25. }  
  26. }while(i<=j); //当两者交错时停止   
  27.   
  28. if(i<right){  
  29. quickSort(strDate,i,right);//从   
  30. }  
  31. if(j>left){  
  32. quickSort(strDate,left,j);  
  33. }  
  34. }  
  35. /** 
  36.   * @param args 
  37.   */  
  38. public static void main(String[] args){  
  39. String[] strVoid=newString[]{"11","66","22","0","55","22","0","32"};  
  40. QuickSort sort=new QuickSort();  
  41. sort.quickSort(strVoid,0,strVoid.length-1);  
  42. for(int i=0;i<strVoid.length;i++){  
  43. System.out.println(strVoid[i]+" ");  
  44. }  
  45. }  
  46.   
  47.   
  48. }  
public class QuickSort {
/**
* 快速排序
* @param strDate
* @param left
* @param right
*/
public void quickSort(String[] strDate,int left,int right){
String middle,tempDate;
int i,j;
i=left;
j=right;
middle=strDate[(i+j)/2];
do{
while(strDate[i].compareTo(middle)<0&& i<right)
i++; //找出左边比中间值大的数
while(strDate[j].compareTo(middle)>0&& j>left)
j--; //找出右边比中间值小的数
if(i<=j){ //将左边大的数和右边小的数进行替换 
tempDate=strDate[i];
strDate[i]=strDate[j];
strDate[j]=tempDate;
i++;
j--;
}
}while(i<=j); //当两者交错时停止

if(i<right){
quickSort(strDate,i,right);//从
}
if(j>left){
quickSort(strDate,left,j);
}
}
/**
  * @param args
  */
public static void main(String[] args){
String[] strVoid=newString[]{"11","66","22","0","55","22","0","32"};
QuickSort sort=new QuickSort();
sort.quickSort(strVoid,0,strVoid.length-1);
for(int i=0;i<strVoid.length;i++){
System.out.println(strVoid[i]+" ");
}
}


}


 

7、有数组a[n],用java代码将数组元素顺序颠倒

  1. //用下面的也可以  
  2.   
  3. //for(int i=0,int j=a.length-1;i<j;i++,j--) 是否等效于 for(int i=0;i<a.length/2;i++)呢?  
  4.   
  5.    
  6.   
  7. importjava.util.Arrays;  
  8.   
  9.    
  10.   
  11. public classSwapDemo{  
  12.   
  13.    
  14.   
  15.    public static void main(String[] args){  
  16.   
  17.       int [] a = new int[]{  
  18.   
  19.                  (int)(Math.random() * 1000),  
  20.   
  21.                  (int)(Math.random()* 1000),  
  22.   
  23.                  (int)(Math.random() * 1000),  
  24.   
  25.                  (int)(Math.random() * 1000),                   
  26.   
  27.                  (int)(Math.random() * 1000)                                                  
  28.   
  29.       };   
  30.   
  31.         
  32.   
  33.       System.out.println(a);  
  34.   
  35.       System.out.println(Arrays.toString(a));  
  36.   
  37.       swap(a);  
  38.   
  39.       System.out.println(Arrays.toString(a));     
  40.   
  41.    }  
  42.   
  43.      
  44.   
  45.    public static void swap(int a[]){  
  46.   
  47.       int len = a.length;  
  48.   
  49.       for(int i=0;i<len/2;i++){  
  50.   
  51.         int tmp = a[i];  
  52.   
  53.         a[i] = a[len-1-i];  
  54.   
  55.         a[len-1-i] = tmp;  
  56.   
  57.       }  
  58.   
  59.    }  
  60.   
  61. }  
//用下面的也可以

//for(int i=0,int j=a.length-1;i<j;i++,j--) 是否等效于 for(int i=0;i<a.length/2;i++)呢?

 

importjava.util.Arrays;

 

public classSwapDemo{

 

   public static void main(String[] args){

      int [] a = new int[]{

                 (int)(Math.random() * 1000),

                 (int)(Math.random()* 1000),

                 (int)(Math.random() * 1000),

                 (int)(Math.random() * 1000),                 

                 (int)(Math.random() * 1000)                                                

      }; 

      

      System.out.println(a);

      System.out.println(Arrays.toString(a));

      swap(a);

      System.out.println(Arrays.toString(a));   

   }

   

   public static void swap(int a[]){

      int len = a.length;

      for(int i=0;i<len/2;i++){

        int tmp = a[i];

        a[i] = a[len-1-i];

        a[len-1-i] = tmp;

      }

   }

}


 

2.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。

  1. 去零的代码:  
  2.   
  3.     returnsb.reverse().toString().replaceAll("零[拾佰仟]","零").replaceAll("零+万","万").replaceAll("零+元","元").replaceAll("零+","零");  
  4.   
  5.    
  6.   
  7. public class RenMingBi {  
  8.   
  9.    
  10.   
  11.        /** 
  12.  
  13.         * @param args add by zxx ,Nov 29, 2008 
  14.  
  15.         */  
  16.   
  17.        private static final char[]data = new char[]{  
  18.   
  19.                      '零','壹','贰','叁','肆','伍','陆','柒','捌','玖'  
  20.   
  21.               };   
  22.   
  23.        private static final char[]units = new char[]{  
  24.   
  25.               '元','拾','佰','仟','万','拾','佰','仟','亿'  
  26.   
  27.        };  
  28.   
  29.        public static voidmain(String[] args) {  
  30.   
  31.               // TODOAuto-generated method stub   
  32.   
  33.               System.out.println(  
  34.   
  35.                             convert(135689123));  
  36.   
  37.        }  
  38.   
  39.    
  40.   
  41.        public static Stringconvert(int money)  
  42.   
  43.        {  
  44.   
  45.               StringBuffer sbf =new StringBuffer();  
  46.   
  47.               int unit = 0;  
  48.   
  49.               while(money!=0)  
  50.   
  51.               {  
  52.   
  53.                      sbf.insert(0,units[unit++]);  
  54.   
  55.                      int number =money%10;  
  56.   
  57.                      sbf.insert(0,data[number]);  
  58.   
  59.                      money /= 10;  
  60.   
  61.               }  
  62.   
  63.    
  64.   
  65.               return sbf.toString();  
  66.   
  67.        }  
  68.   
  69. }  
原文地址:https://www.cnblogs.com/gxpblogs/p/3068819.html