java部分算法例题

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

doubleNum(n*2);

 

 

System.out.println(n);

 

 

 

}

 

 


 

递归算法题

2

1

个人

10

,第

2

个比第

1

个人大

2

岁,依次递推,请用递归方式计算出第

8

个人多大?

 

package

cn.itcast;

 

import

java.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;

 

}

 

 

 

}

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

 

 

 

 

 

 

public

 

static

 

void

toBinary(

int

n,StringBuffer result)

 

{

 

 

 

if

(n/2 != 0)

 

 

 

toBinary

(n/2,result);

 

 

result.append(n%2);

 

 

 

}

 

排序都有哪几种方法?请列举。用

JA

V

A

实现一个快速排

序。

 

 

 

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

 

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=new

String[]{"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]+"

");

}

}

 

 

}

有数组

a[n]

,用

java

代码将数组元素顺序颠倒

 

//

用下面的也可以

 

//

for(int i=0,int j=a.length-1;i<j;i++,j--)

是否等效于

for(int i=0;i<a.length/2;i++)

呢?

 

 

import java.util.Arrays;

 

public class SwapDemo{

 

 

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;

 

 

}

 

}

}

金额转换,阿拉伯数字的金额转换成中国传统的形式如:

(¥

1011

)-

>

(一千零一拾一元整)输出。

 

去零的代码:

 

 

return

sb.reverse().toString().replaceAll("

[

]","

").replaceAll("

+

","

").replaceAll("

+

","

").replaceAll("

+","

");

 

 

public class RenMingBi {

 

 

/**

 

 

* @param args add by zxx ,Nov 29, 2008

 

 

*/

 

private static final char[] data = new char[]{

 

 

 

'

','

','

','

','

','

','

','

','

','

'

 

 

};

 

 

private static final char[] units = new char[]{

 

 

'

','

','

','

','

','

','

','

','

亿

'

 

};

 

public static void main(String[] args) {

 

 

// TODO Auto-generated method stub

 

 

System.out.println(

 

 

 

 

convert(135689123));

 

}

 

 

public static String convert(int money)

 

{

 

 

 

 

 

 

StringBuffer sbf = new StringBuffer();

 

 

int unit = 0;

 

 

while(money!=0)

 

 

{

 

 

 

sbf.insert(0,units[unit++]);

 

 

 

int number = money%10;

 

 

 

sbf.insert(0, data[number]);

 

 

 

money /= 10;

 

 

}

 

 

 

return sbf.toString();

 

}

}

 

posted @ 2013-10-28 18:59  逍遥神  阅读(278)  评论(1编辑  收藏  举报