天天看点

面试算法题4

版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1562576

 面试算法题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