一个整数,大于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 |
第1个人10,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
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); }
本人只研究过冒泡排序、选择排序和快速排序,下面是快速排序的代码:
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]+" "); } } }
//用下面的也可以 //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; } } }
去零的代码: returnsb.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 voidmain(String[] args) { // TODOAuto-generated method stub System.out.println( convert(135689123)); } public static Stringconvert(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(); } }