9kbo69nxp4 发表于 2016-6-6 07:07:36

华为机考

  1. 判断回文
  public class Huiwen {

    static boolean IsHuiwen(String org){
        StringBuffer strBf = new StringBuffer(org);
        StringBuffer strBfv = strBf.reverse();
        System.out.println("Org:"+org);
        System.out.println("OrgRe:"+strBfv);
        return org.equals(strBfv.toString());  //一定要用org,不能用strBf
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println(IsHuiwen("abcba"));
    }

}
  2. 求数组中大于平均值的个数
  3. 单链表逆置
  
public class LinkReserve {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        class Node{
            String value;
            Node next;
           
            public Node(String value){
                this.value = value;
            }
           
        }
        Node n1 = new Node("a");
        Node n2 = new Node("b");
        Node n3 = new Node("c");
        Node n4 = new Node("d");
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
       
        Node head = n1;
        Node pre = null;
        Node next = head.next;
        head.next = pre;
       
        while(next != null){
            pre = head;
            head = next;
            next = next.next;
            head.next = pre;
           
        }

        Node p = head;
        while(p != null){
            System.out.println(p.value);
            p = p.next;
        }

    }

}
4. 树的遍历
  5. 大数相加
  
  
[*]import java.io.BufferedReader;  
[*]import java.io.IOException;  
[*]import java.io.InputStreamReader;  
[*]import java.util.StringTokenizer;  
[*]  
[*]public class BigIntegerOperation {      
[*]      
[*]      
[*]    public static void main(String[] args) throws IOException {  
[*]          
[*]        /** 
[*]        * 从控制台读取输入数据 格式为 num1空格num2 为空则exit 
[*]        */  
[*]        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
[*]        String str = in.readLine();  
[*]        while(str!=null){  
[*]        StringTokenizer st = new StringTokenizer(str," ");  
[*]        String num1 = st.nextToken();  
[*]        String num2 = st.nextToken();  
[*]        int len1 = num1.length();  
[*]        int len2 = num2.length();  
[*]        /** 
[*]        * 将不等长的两输入字符串格式化,比如 num1 = 123 num2 = 1234 
[*]        * 经格式化后将成为num1 = 0123 num2 = 1234为方便计算 
[*]        */  
[*]        if(len1 > len2){  
[*]            for(int i=0; i< len1-len2; i++){  
[*]                num2 = "0"+num2;  
[*]            }  
[*]        }else if(len2>len1){  
[*]            for(int i=0; i< len2-len1; i++){  
[*]                num1 = "0"+num1;  
[*]            }  
[*]        }  
[*]          
[*]        int[] arr1 = BigIntegerOperation.str2intArr(num1);  
[*]        int[] arr2 = BigIntegerOperation.str2intArr(num2);  
[*]          
[*]        arr1 = BigIntegerOperation.reverse(arr1);  
[*]        arr2 = BigIntegerOperation.reverse(arr2);  
[*]          
[*]          
[*]        int[] result = BigIntegerOperation.add(arr1, arr2);  
[*]          
[*]//        System.out.print(num1 +" + " + num2 +" = ");  
[*]        for(int i=result.length-1; i>=0; i--){  
[*]            if(i == result.length-1 && result == 0) continue;  
[*]            System.out.print(result);  
[*]        }  
[*]        str = in.readLine();  
[*]        }  
[*]    }  
[*]      
[*]    /** 
[*]    * 倒置数组元素 
[*]    */  
[*]    public static int [] reverse(int[] arr){  
[*]        int len = arr.length;  
[*]        for(int i = 0,j = len-1; i<j; i++,j--){  
[*]            int temp = arr;  
[*]            arr = arr;  
[*]            arr = temp;  
[*]        }  
[*]        return arr;  
[*]    }  
[*]      
[*]    /** 
[*]    *     将字符串转换成整型数组 
[*]    */  
[*]    public static int [] str2intArr(String str){  
[*]        int len = str.length();  
[*]        int [] arr = new int;  
[*]        for(int i = 0 ; i<len; i++){  
[*]            arr = str.charAt(i) - '0' ;  
[*]        }  
[*]        return arr;  
[*]    }  
[*]      
[*]    /** 
[*]    * 核心方法   两个整型数组相加      
[*]    */  
[*]    public static int [] add(int a[], int b[]){  
[*]        int maxLen = a.length;  
[*]        int[] sum = new int1];  
[*]          
[*]        for(int i = 0; i< maxLen ; i++){  
[*]            int tempSum = a + b;  
[*]            sum += tempSum%10;  
[*]            int d = tempSum/10; //进位  
[*]            sum1] += d;              
[*]        }  
[*]        return sum;  
[*]    }  
[*]  
[*]}  
页: [1]
查看完整版本: 华为机考