论坛首页 Java企业应用论坛

一道面试排序题

浏览 35538 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (4) :: 隐藏帖 (3)
作者 正文
   发表时间:2010-06-12  
public static int reverse(int sum, int n){
        sum = sum*10;
        sum += n%10;
        if(n/10 != 0)
            sum = reverse(sum, n/10);
        return sum;
    }
0 请登录后投票
   发表时间:2010-06-12   最后修改:2010-06-12
public static int reverse(int number,int result) { 
      if(number==0) 
           return result; 
      return reverse(number/10, result*10+number%10); 
} 
public static void main(String[] args) { 
         System.out.println(reverse(1202434,0)); 
}

 

 

 

0 请登录后投票
   发表时间:2010-06-12  
一行
public class Invert {
	public static void main(String[] args) {
		int n=12342;
		int m=invert(n,0);
		System.out.println(m);
	}

	private static int invert(int n, int r) {
		return n>9? invert(n/10, r*10+n%10): r*10+n;
	}
}

0 请登录后投票
   发表时间:2010-06-12  
@Test
	public void testReverse() {
		println(reverse(12345));
		println(reverse2(12345));
	}
	
	private int reverse(int number) {
		char[] olds = ("" + number).toCharArray();
		char[] news = new char[olds.length];
		for (int i = 0; i < olds.length; i++) news[olds.length - i - 1] = olds[i];
		return Integer.parseInt(new String(news));
	}
	
	private int reverse2(int number) {
		char[] olds = ("" + number).toCharArray();
		StringBuffer buf = new StringBuffer();
		for (int i = olds.length - 1; i >= 0 ; i--) buf.append(olds[i]);
		return Integer.parseInt(buf.toString());
	}
0 请登录后投票
   发表时间:2010-06-21  
随便写了一个递归的

public class Test {

	/**
	 * @param args
	 *            time: 2010-6-21 上午07:17:23
	 * @author: rui.zhang
	 */
	public static void main(String[] args) {
		Test tt = new Test();
		int result = tt.convert(123456789);
		System.out.println(result);

	}

	private int convert(int input) {
		String sou = "" + input;
		if (sou.length() == 1) {
			return Integer.parseInt(sou);
		} else if (sou.length() == 2) {
			return Integer.parseInt(sou.substring(1) + sou.substring(0, 1));
		} else {
			return Integer.parseInt(sou.substring(sou.length() - 1)
					+ convert(Integer.parseInt(sou.substring(1,
							sou.length() - 1))) + sou.substring(0, 1));
		}
	}

}


0 请登录后投票
   发表时间:2010-06-21   最后修改:2010-06-21
public class Test2 {
	public static void main(String[] args) {
		System.out.println(reverse(12345));
	}

	public static int reverse(int number) {
		int c = number / 10;
		int temp = number % 10;
		if (c != 0) {
			return Integer.valueOf("" + temp + reverse(c));
		}
		return temp;
	}

}

 

 

欢迎拍砖

0 请登录后投票
   发表时间:2010-06-21  
elmar 写道
一行
public class Invert {
	public static void main(String[] args) {
		int n=12342;
		int m=invert(n,0);
		System.out.println(m);
	}

	private static int invert(int n, int r) {
		return n>9? invert(n/10, r*10+n%10): r*10+n;
	}
}


掩人耳目……
0 请登录后投票
   发表时间:2010-06-21  
jeanron 写道
public class Test {
public static void main(String[] args) {
System.out.println(Test.reverseInt(123456));
}

public static int reverseInt(int temp){
StringBuffer str = new StringBuffer();
str.append(temp).reverse();
return Integer.parseInt(str.toString());
}
}

StringBuffer 有现成的reverse()方法~~

没看清题目……
0 请登录后投票
   发表时间:2010-06-22   最后修改:2010-06-22
问我散,1行代码。。。


public static String reString(String sss) {//这行不算
		return sss==null||sss.length()<=1?sss:sss.charAt(sss.length()-1)+reString(sss.substring(0,sss.length()-1));
	}//后括号不算一行


int的话


public static int reInt(int n) {
		return n<10?n:(n%10)*((int)java.lang.Math.pow(10, (n+"").length()-1))+reInt(n/10);
	}

1 请登录后投票
   发表时间:2010-06-22  
static int rev3(int inputNum){
if(inputNum<10) return inputNum;
int l=inputNum/10;
int r=inputNum%10;
return new Integer(r+""+rev3(l));
}
0 请登录后投票
论坛首页 Java企业应用版

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