论坛首页 Java企业应用论坛

一道面试排序题

浏览 35539 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (4) :: 隐藏帖 (3)
作者 正文
   发表时间:2011-04-12  
public class Paixu {

/**
* @param args
*  12345
*
*/
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5,6,7,5 };
stack(0, arr);
}

public static int stack(int i, int arr[]) {
if(i!=arr.length-1)
stack(i +1 , arr);
System.out.print(arr[i]);
return 0;
}

}
0 请登录后投票
   发表时间:2011-04-12  
请楼上两位注意,方法输入是一个整数,输出也是一个整数。
0 请登录后投票
   发表时间:2011-04-12   最后修改:2011-04-12
没测试过,不知道对不对,先放着

public static int r(int x){
  if(x < 10){
    return x;
  }
  int digits = (int)Math.floor(Math.log10(x));//实际位数减1

  int first = (int)Math.floor(x / Math.power(10,digits));//找首位数字,203就得到2

  int removeFirst = x - (int)first * Math.power(10,digits);//去掉首位,203就得到3
 
  int step = digits  - (int)Math.floor(Math.log10(removeFirst ));//目的是处理数字中间的0,以203为例,step=2
 
  return r(removeFirst)*Math.power(10,step) + first;
}
0 请登录后投票
   发表时间:2011-05-19  
public class Testdigui {

public static void writeBackward(String s, int size) {

if (size > 0) {
System.out.print(s.substring(size - 1, size));
writeBackward(s, size - 1);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 12345;
Testdigui tt = new Testdigui();
String s = String.valueOf(i);
int size = s.length();
;

tt.writeBackward(s, size);
}

}
0 请登录后投票
   发表时间:2011-05-19   最后修改:2011-05-19
public  static void alertValues(int i){
String value=String.valueOf(i);
if(value.length()>1){
System.out.print(value.substring(value.length()-1));
value = value.substring(0,value.length()-1);
alertValues(Integer.valueOf(value));
}else System.out.print(value);
}

public static void main(String[] args) {
alertValues(112344448);
}
0 请登录后投票
   发表时间:2011-05-19  

package qa;

/**
 * 题目:写一个方法,要求参数int类型,如:传入一个12345,返回结果54321。
 * 要求,是方法体内的代码不能超过8行,而且还要用递归。
 * http://www.iteye.com/topic/683454
 * @author syi
 *
 */
public class Q4 {
	public static void main(String[] args) {
		System.out.println(getStr(789076));
	}
	
	public static String getStr(int num){
		StringBuffer sb = new StringBuffer();
		if(num > 10){
			sb.append(String.valueOf(num % 10));
			num = num / 10;
			sb.append(getStr(num));
		}else{
			 return String.valueOf(num);
		}
		return sb.toString();
	}
}


0 请登录后投票
   发表时间:2011-05-19  
sy197661944 写道

package qa;

/**
 * 题目:写一个方法,要求参数int类型,如:传入一个12345,返回结果54321。
 * 要求,是方法体内的代码不能超过8行,而且还要用递归。
 * http://www.iteye.com/topic/683454
 * @author syi
 *
 */
public class Q4 {
	public static void main(String[] args) {
		System.out.println(getStr(789076));
	}
	
	public static String getStr(int num){
		StringBuffer sb = new StringBuffer();
		if(num > 10){
			sb.append(String.valueOf(num % 10));
			num = num / 10;
			sb.append(getStr(num));
		}else{
			 return String.valueOf(num);
		}
		return sb.toString();
	}
}






缩短点....

package qa;


	public static String getStr(int num){
		if(num > 10){
			return String.valueOf(num % 10) + getStr(num / 10);
		}else{
			 return String.valueOf(num);
		}
	}


0 请登录后投票
   发表时间:2011-05-20  

public class Test {
	public static int inverse(int a ){
		
		int x = a % 10;
		if( a / 10 == 0){
			return new Integer(new Integer(x).toString());
		}
		else{
			return new Integer(new Integer(x).toString() + inverse(a/10));
		}
		
		
	}
		
	public static void main(String args[]){
		
		System.out.println( inverse(12345) );
		
	}
}

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics