您的位置:首页 > 职场人生

面试算法题4

2007-04-12 19:27 190 查看
面试算法题4
据说是华为笔试题,练习了一下。

package src;
/*
* 第二题: 假设有个int型的数n,实现高低位的互换,比如12345,给换成54321,写一个方法实现n的高低位的互换。(n是几不知道)
*/
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Mymethod5 {
public static Integer n;
public static void main(String[] args) throws NumberFormatException, IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一整数: ");
n=Integer.parseInt(br.readLine());
String str=n.toString();
Pattern p=Pattern.compile("[//d]+");
Matcher m=p.matcher(str);
char temp;
if(!m.matches()){
System.out.println("输入数据有误,请核对!!");
}else{
char[] ch=str.toCharArray();
for(int i=0;i<ch.length/2;i++){
temp=ch[i];
ch[i]=ch[ch.length-i-1];
ch[ch.length-i-1]=temp;
}
System.out.println(ch);
}

}
}
---------------------------------

输出结果:

请输入一整数:
25436321
12363452
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: