华为上机测试题(20160725)

随便写的不知道正不正确

package mh.test.common.autocloseable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Test {

    public static void main(String[] args) {
        System.out.println("华为第三题,一二题在文件中");
        new Test().test3();
    }

    // 测试题1
    public void test1() {
        Scanner cin = new Scanner(System.in);
        int n;
        long countResult = 2;
        String hexResult = "";
        if (cin.hasNext()) {
            n = cin.nextInt();
            if (n <= 31) {
                countResult <<= (n - 1);
                hexResult = Long.toHexString(countResult);
            }
            System.out.println("0x" + hexResult);
        }

    }

    // 测试题2
    public void test2() {
        Scanner cin = new Scanner(System.in);
        String presentString = "";
        while (cin.hasNextLine()) {
            presentString = cin.nextLine();
            int plen = presentString.length();
            int depth = 0;
            for (int i = 0; i < plen; i++) {

                if (presentString.charAt(i) == '(') {
                    depth = depth + 1;
                } else if (presentString.charAt(i) == ')') {
                    depth = depth - 1;
                } else {
                    System.out.println(depth);
                    break;
                }
            }
        }
    }

    // 测试题3
    public void test3() {
        Scanner cin = new Scanner(System.in);
        String presentString = "";
        boolean isNewTree = true;
        Node rootNode = new Node();
        // 树的根节点
        rootNode.value = "1";
        // 所以节点的Map<节点的值,节点的Node对象>
        HashMap<String, Node> nodHashMap = new HashMap<>();
        // 将根节点放入Map中
        nodHashMap.put("1", rootNode);
        // 所有的边的列表
        ArrayList<Edge> edgesList = new ArrayList<>();
        // 循环检查输入的树
        while (cin.hasNext()) {
            // 构建新树
            rootNode.chiArrayList.clear(); // 清空所有的节点
            nodHashMap.clear(); // 清空所有的节点
            edgesList.clear(); // 清空所以的边

            int k = cin.nextInt();
            cin.nextLine(); // 清空输入缓冲
            isNewTree = false;
            for (int i = 0; i < k - 1; i++) {
                // System.err.println(k);
                if (cin.hasNext()) {
                    String v = cin.nextLine();
                    // System.out.println(v);
                    String node[] = v.split(" ");
                    // 添加新的边到List中
                    Edge nEdge = new Edge(node[0], node[1]);
                    edgesList.add(nEdge);
                    // System.out.println(node[0]+"+"+node.length);
                    // 构建树
                    if (!nodHashMap.containsKey(node[0])) {
                        Node newNode = new Node();
                        Node chiNode = new Node();
                        newNode.value = node[0];
                        chiNode.value = node[1];
                        newNode.chiArrayList.add(chiNode);
                        chiNode.fatherNode = newNode;
                        nodHashMap.put(node[1], chiNode);
                        nodHashMap.put(node[0], newNode);
                    } else {
                        Node fatherNode = nodHashMap.get(node[0]);
                        Node chiNode = null;
                        if (!nodHashMap.containsKey(node[1])) {
                            chiNode = new Node();
                            chiNode.value = node[1];
                            nodHashMap.put(node[1], chiNode);
                        } else {
                            chiNode = nodHashMap.get(node[1]);
                        }
                        chiNode.fatherNode = fatherNode;
                        fatherNode.chiArrayList.add(chiNode);

                    }
                }

            }
            // printTree(rootNode);
            String searchSeq = cin.nextLine();
            String sArray[] = searchSeq.split(" ");
            // System.out.println("叶子节点顺序:"+searchSeq+","+sArray.length);
            // 第一个叶子节点到根路径的所有边
            Node firstNode = nodHashMap.get(sArray[0]);
            Node tempNode = firstNode;
            // searchPaht
            LinkedList<String> nodeArrayList = new LinkedList<>();
            // 所有边的全职加一
            nodeArrayList.addFirst(firstNode.value);
            while (tempNode.fatherNode != null) {
                // System.out.println("遍历第一个节点");
                Node faNode = tempNode.fatherNode;
                // 路径的节点信息
                nodeArrayList.addFirst(faNode.value);
                Edge temp = new Edge(tempNode.value, faNode.value);
                for (int i = 0; i < edgesList.size(); i++) {
                    Edge comEdge = edgesList.get(i);
                    if (comEdge.equals(temp)) {
                        comEdge.count++;
                    }
                }
                tempNode = faNode;
            }
            if (sArray.length == 1) {
                // System.out.println("只有一个叶节点");
                LinkedList<String> tempPathList = new LinkedList<>();
                String leaveNodeString = nodeArrayList.removeLast();
                tempPathList.addFirst(leaveNodeString);
                while (nodeArrayList.size() != 0) {
                    String tempNodeString = nodeArrayList.removeLast();
                    tempPathList.addFirst(tempNodeString);
                    tempPathList.addLast(tempNodeString);
                }
                for (int i = 0; i < edgesList.size(); i++) {
                    Edge teEdge = edgesList.get(i);
                    teEdge.count++;
                }
                nodeArrayList = tempPathList;
            } else {
                // System.out.println("遍历其他叶节点");
                // printPath(nodeArrayList);
                tempNode = firstNode;
                for (int l = 1; l < sArray.length; l++) {
                    Node seNode = nodHashMap.get(sArray[l]);
                    // 获取到共同的父节点
                    Node commonFather = getSameFatherNode(tempNode, seNode);
                    Node samTempNode = tempNode;
                    // System.out.println("遍历第"+l+"节点"+tempNode.value+","+seNode.value+","+commonFather.value);
                    // 计算当前节点到共同父亲的路径
                    while (samTempNode.fatherNode != null) {
                        Node faNode = samTempNode.fatherNode;
                        nodeArrayList.addLast(faNode.value);
                        Edge temp = new Edge(samTempNode.value, faNode.value);
                        for (int i = 0; i < edgesList.size(); i++) {
                            Edge comEdge = edgesList.get(i);
                            if (comEdge.equals(temp)) {
                                comEdge.count++;
                            }
                        }
                        if (samTempNode == commonFather) {
                            break;
                        }
                        samTempNode = faNode;
                    }
                    // 计算下一个节点到共同父亲的路径
                    samTempNode = seNode;
                    LinkedList<String> tempNLinkedList = new LinkedList<>();
                    tempNLinkedList.addFirst(seNode.value);
                    while (samTempNode.fatherNode != null) {
                        // System.out.println("111111111");
                        Node faNode = samTempNode.fatherNode;
                        Edge temp = new Edge(samTempNode.value, faNode.value);
                        for (int i = 0; i < edgesList.size(); i++) {
                            Edge comEdge = edgesList.get(i);
                            if (comEdge.equals(temp)) {
                                comEdge.count++;
                            }
                        }
                        if (faNode == commonFather) {
                            break;
                        } else {
                            tempNLinkedList.addFirst(faNode.value);
                        }
                        samTempNode = faNode;
                    }
                    while (tempNLinkedList.size() != 0) {
                        nodeArrayList.addLast(tempNLinkedList.removeFirst());
                    }
                    // 继续往下一个节点计算
                    tempNode = seNode;
                    // System.out.println("mmmmmmmmmmm");
                }

            }
            // 验证边的访问次数是否都为2
            ArrayList<Edge> oneCountEdgeList = new ArrayList<>();
            String nextNode = getNextNode(tempNode.value, edgesList);
            while (nextNode != null) {
                nodeArrayList.addLast(nextNode);
                nextNode = getNextNode(nextNode, edgesList);
            }
            // 输出边的访问次数和节点访问路径
            // System.out.println("输出结果");
            printEdgeListCount(edgesList);
            printPath(nodeArrayList, edgesList);
        }
    }

    // 节点的类
    class Node {
        Node fatherNode;
        ArrayList<Node> chiArrayList = new ArrayList<>();
        String value;

        public ArrayList<Node> getChiArrayList() {
            return chiArrayList;
        }

        public void setChiArrayList(ArrayList<Node> chiArrayList) {
            this.chiArrayList = chiArrayList;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

    }

    // 边的类
    class Edge {
        String node1;
        String node2;
        int count = 0;

        public Edge(String node1, String node2) {
            // TODO Auto-generated constructor stub
            this.node1 = node1;
            this.node2 = node2;
        }

        public String getNode1() {
            return node1;
        }

        public void setNode1(String node1) {
            this.node1 = node1;
        }

        public String getNode2() {
            return node2;
        }

        public void setNode2(String node2) {
            this.node2 = node2;
        }

        @Override
        public boolean equals(Object obj) {
            Edge edge = (Edge) obj;
            // TODO Auto-generated method stub
            if (this.node1.equals(edge.getNode1())
                    && this.node2.equals(edge.getNode2())) {
                return true;
            } else if (this.node1.equals(edge.getNode2())
                    && this.node2.equals(edge.getNode1())) {
                return true;
            }
            return false;
        }

        public boolean isContainNode(String node) {
            if (node1.equals(node) || node2.equals(node))
                return true;
            return false;
        }

        public String getAnotherNode(String node) {
            String anotherNode = null;
            if (node1.equals(node)) {
                anotherNode = node2;
            } else if (node2.equals(node)) {
                anotherNode = node1;
            }
            return anotherNode;
        }
    }

    public static Node getSameFatherNode(Node node1, Node node2) {
        // System.out.println("获取共同的父节点");
        Node commonFatherNode = null;
        HashMap<Node, Integer> fatherMap = new HashMap<>();
        Node tempNode = node1;
        while (tempNode.fatherNode != null) {
            // System.out.println("ffffff("+node1.value+","+node2.value+")"+tempNode.value);
            Node tempfaNode = tempNode.fatherNode;
            if (!fatherMap.containsKey(tempfaNode)) {
                fatherMap.put(tempfaNode, 1);
            }
            tempNode = tempfaNode;
        }
        tempNode = node2;
        while (tempNode.fatherNode != null) {
            // System.out.println("sssssssssssssssssssssssssssss");
            Node tempfaNode = tempNode.fatherNode;
            if (fatherMap.containsKey(tempfaNode)) {
                commonFatherNode = tempfaNode;
                break;
            }
            tempNode = tempfaNode;
        }
        return commonFatherNode;
    }

    public static void printTree(Node rootNode) {
        System.out.println("输出构建的数的结构");
        LinkedList<Node> linkedList = new LinkedList<>();
        if (rootNode != null) {
            linkedList.addLast(rootNode);
            while (linkedList.size() != 0) {
                Node tempNode = linkedList.removeFirst();
                System.out.println(tempNode.value);
                ArrayList<Node> childArrayList = tempNode.chiArrayList;
                if (childArrayList.size() != 0) {
                    for (int i = 0; i < childArrayList.size(); i++) {
                        linkedList.addLast(childArrayList.get(i));
                        // System.out.print(childArrayList.get(i).value+" ");
                    }
                    // System.out.println();
                }
            }
        }
        System.out.println();
    }

    public static void printPath(LinkedList<String> path,
            ArrayList<Edge> eArrayList) {
        System.out.print("输出节点路径:");
        for (int i = 0; i < path.size(); i++) {
            System.out.print(path.get(i) + " ");
        }
        System.out.println();
    }

    public static void printEdgeListCount(ArrayList<Edge> edges) {
        System.out.println("输出所有边访问次数");
        boolean flag = true;
        for (int i = 0; i < edges.size(); i++) {
            Edge tempEdge = edges.get(i);
            System.out.println("(" + tempEdge.node1 + "," + tempEdge.node2
                    + ")" + "=" + tempEdge.count);
            if (tempEdge.count != 2) {
                flag = false;
            }
        }
        System.out.println();
        System.out.println("遍历结果:" + (flag == true ? 0 : -1));
    }

    // 检查是否有边的访问次数没有超过2
    public static String getNextNode(String lefeNode,
            ArrayList<Edge> elArrayList) {
        String nextNode = null;
        for (int i = 0; i < elArrayList.size(); i++) {
            Edge tempEdge = elArrayList.get(i);
            if (tempEdge.isContainNode(lefeNode)) {
                if (tempEdge.count < 2) {
                    nextNode = tempEdge.getAnotherNode(lefeNode);
                    tempEdge.count++;
                }
            }
        }
        return nextNode;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值