论坛首页 Java企业应用论坛

一道面试排序题

浏览 35542 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (4) :: 隐藏帖 (3)
作者 正文
   发表时间:2010-06-05  
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()方法~~
1 请登录后投票
   发表时间:2010-06-05   最后修改:2010-06-05
public class Test {

	public static void main(String[] args) {
		System.out.println(reverse("",1234021));
	}
	
	public static String reverse(String prefix,int d){
		if(d<10){
			return prefix+d;
		}else{
			return reverse(prefix+(d%10),d/10);
		}
	}
	
}

 

0 请登录后投票
   发表时间:2010-06-05  
public int reverse_num(int ori_num, int res_num) {
    int f_num = ori_num / 10;
    int l_num = ori_num % 10;
    if ( f_num == 0 )
       return res_num * 10 + l_num;
    else
       return reverse_num( f_num, l_num * 10 );
}
0 请登录后投票
   发表时间:2010-06-07  
public class DataFlip {

public static void main(String[] args) {
DataFlip dataFlip = new DataFlip();
StringBuffer returnValue = new StringBuffer();
dataFlip.recursive(1234567,7,returnValue);
System.out.println(returnValue);
}

/**
*
* @param source 
* @param length
* @param returnValue
*/
public void recursive(int source,int length,StringBuffer returnValue) {
if (length == 1) {
returnValue.append(source);
} else {
String str = source + "";
returnValue.append(str.substring(length - 1,length));
str = str.substring(0,length - 1);
this.recursive(Integer.parseInt(str),str.length(),returnValue);
}
}

}
0 请登录后投票
   发表时间:2010-06-07  
public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		int n=123456;
		int m=0;
		while(n>0)
		{
			m=m*10+n%10;
			n=n/10;
		}
		
		System.out.println(m);	
	}
0 请登录后投票
   发表时间:2010-06-08  
	public int fn2(int i)
	{
		if(i >10)
			return (i%10)*(int)Math.pow(10,String.valueOf(i).length()-1)+Test.fn2(i/10);
		else
			return i;
	}
0 请登录后投票
   发表时间:2010-06-10  
public static void reverse(int num){
String nu=""+num;
if(nu.length()==1){
System.out.print(nu);
return ;
}
reverse(Integer.parseInt(nu.substring(1)));
System.out.print(nu.charAt(0));
}
0 请登录后投票
   发表时间:2010-06-10  
	static int i=0;
	public static void main(String[] args) throws Exception {
	               System.out.println(reverse2(123456008));
	}
	public static int reverse2(int num){
		if(num<10){
			i=0;
			return num;
		}
		return (int)(reverse2(num/10)+(num%10)*Math.pow(10, ++i));
	}

 

0 请登录后投票
   发表时间:2010-06-11  
 public static void doit(int src,int i,char[] c){   
        if(i==0)   
            c = new char[(src+"").length()];   
       c[(src+"").length()-1-i] = (src+"").charAt(i);   
       if(i==(src+"").length()-1){   
            System.out.println(Integer.parseInt(new String(c)));   
            return;   
        }   

        doit(src,i+1,c);   
}



不懂为啥非要递归。
直接对应首尾换位不就好了么……
0 请登录后投票
   发表时间:2010-06-11  

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(inverse(12345));
                System.out.println(inverse(123450));
	}

	
	public static int inverse(int a){
		int len = String.valueOf(a).length();
		int b = (int) ((a - ((int)(a/10))*10) * Math.pow(10, len-1));
		if(len > 1){
			b += inverse((int)(a/10));
		}
		return b;
	}
}
 
0 请登录后投票
论坛首页 Java企业应用版

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