面试算法题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
据说是华为笔试题,练习了一下。
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
相关文章推荐
- 今天看到别人的面试算法题,求找出十包粉末中两包蓝色粉末的最短时间
- 微软面试、经典算法、编程艺术、红黑树4大系列总结
- LeetCode题目难度分布(含面试频率及使用的数据结构与算法)
- 字符串匹配算法 【微软面试100题 第三十三题】
- 面试算法题1
- Java求职面试准备之常见算法
- [答案V0.2版]精选微软等数据结构+算法面试100题[1-20]
- Java工程师面试---面试算法题(一)
- 面试算法题2
- 算法面试:精选微软等公司经典的算法面试100题 第1-40题
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
- 面试算法题3
- 面试算法:镜像二叉树的检测
- 面试算法题6
- 编程面试的10大算法概念汇总
- 编程面试的10大算法概念汇总
- 经典算法面试与解答(二)
- 编程面试的10大算法概念汇总
- leetcode:Reorder List (修改链表排列不改变值) 【面试算法题】
- 编程面试的10大算法概念汇总