深圳同城快跑笔试题目 3 实现四则运算

package com.cici.深圳同城快跑;

import java.util.Stack;

class Node
{
public char cData;              // data item (key)
public Node leftChild;         // this node‘s left child
public Node rightChild;        // this node‘s right child

public void displayNode()      // display ourself
   {
   System.out.print(‘{‘);
   System.out.print(cData);
   System.out.print("} ");
   }
}  // end class Node
class Tree
{
public Node root;             // first node of tree
public int size;
//-------------------------------------------------------------
public Tree()                  // constructor
   { root = null; }            // no nodes in tree yet
//-------------------------------------------------------------
public Node find(char key)      // find node with given key
   {                           // (assumes non-empty tree)
   Node current = root;               // start at root
   while(current.cData != key)        // while no match,
      {
      if(key < current.cData)         // go left?
         current = current.leftChild;
      else                            // or go right?
         current = current.rightChild;
      if(current == null)             // if no child,
         return null;                 // didn‘t find it
      }
   return current;                    // found it
   }  // end find()
//-------------------------------------------------------------
public void insert(char c)
{
Node newNode = new Node();    // make new node
newNode.cData = c;           // insert data
if(root==null)                // no node in root
   root = newNode;
else                          // root occupied
   {
    //make a new node which is stands for the new node
   Node current = root;       // start at root
   Node parent;
   while(true)                // (exits internally)
      {
       //parent node is the root node
      parent = current;
      //go left ?
      if(check(c)){
          current = current.leftChild;
          if(current == null)  // if end of the line,
             {                 // insert on left
             parent.leftChild = newNode;
             return;
             }
      } // end if go left
      else                    // or go right?
         {
         current = current.leftChild;
         if(current == null)  // if end of the line
            {                 // insert on right
            parent.rightChild = newNode;
            return;
            }
         }  // end else go right
      }  // end while
   }  // end else not root
}  // end insert()
//-------------------------------------------------------------
public boolean delete(char key) // delete node with given key
   {                           // (assumes non-empty list)
   Node current = root;
   Node parent = root;
   boolean isLeftChild = true;

   while(current.cData != key)        // search for node
      {
      parent = current;
      if(key < current.cData)         // go left?
         {
         isLeftChild = true;
         current = current.leftChild;
         }
      else                            // or go right?
         {
         isLeftChild = false;
         current = current.rightChild;
         }
      if(current == null)             // end of the line,
         return false;                // didn‘t find it
      }  // end while
   // found node to delete

   // if no children, simply delete it
   if(current.leftChild==null &&
                                current.rightChild==null)
      {
      if(current == root)             // if root,
         root = null;                 // tree is empty
      else if(isLeftChild)
         parent.leftChild = null;     // disconnect
      else                            // from parent
         parent.rightChild = null;
      }

   // if no right child, replace with left subtree
   else if(current.rightChild==null)
      if(current == root)
         root = current.leftChild;
      else if(isLeftChild)
         parent.leftChild = current.leftChild;
      else
         parent.rightChild = current.leftChild;

   // if no left child, replace with right subtree
   else if(current.leftChild==null)
      if(current == root)
         root = current.rightChild;
      else if(isLeftChild)
         parent.leftChild = current.rightChild;
      else
         parent.rightChild = current.rightChild;

   else  // two children, so replace with inorder successor
      {
      // get successor of node to delete (current)
      Node successor = getSuccessor(current);

      // connect parent of current to successor instead
      if(current == root)
         root = successor;
      else if(isLeftChild)
         parent.leftChild = successor;
      else
         parent.rightChild = successor;

      // connect successor to current‘s left child
      successor.leftChild = current.leftChild;
      }  // end else two children
   // (successor cannot have a left child)
   return true;                                // success
   }  // end delete()
//-------------------------------------------------------------
// returns node with next-highest value after delNode
// goes to right child, then right child‘s left descendents
private Node getSuccessor(Node delNode)
   {
   Node successorParent = delNode;
   Node successor = delNode;
   Node current = delNode.rightChild;   // go to right child
   while(current != null)               // until no more
      {                                 // left children,
      successorParent = successor;
      successor = current;
      current = current.leftChild;      // go to left child
      }
                                        // if successor not
   if(successor != delNode.rightChild)  // right child,
      {                                 // make connections
      successorParent.leftChild = successor.rightChild;
      successor.rightChild = delNode.rightChild;
      }
   return successor;
   }
//-------------------------------------------------------------
public void traverse(int traverseType)
   {
   switch(traverseType)
      {
      case 1: System.out.print("\nPreorder traversal: ");
              preOrder(root);
              break;
      case 2: System.out.print("\nInorder traversal:  ");
              inOrder(root);
              break;
      case 3: System.out.print("\nPostorder traversal: ");
              postOrder(root);
              break;
      }
   System.out.println();
   }
//-------------------------------------------------------------
public void preOrder(Node localRoot)
   {
   if(localRoot != null)
      {
      preOrder(localRoot.leftChild);
      System.out.print(localRoot.cData + " ");
      preOrder(localRoot.rightChild);
      }
   }
//-------------------------------------------------------------
public Node inOrder(Node localRoot)
   {
   if(localRoot != null)
      {
      inOrder(localRoot.leftChild);
      System.out.print(localRoot.cData + " ");
      inOrder(localRoot.rightChild);
      }
   return localRoot;
   }
//-------------------------------------------------------------
public void postOrder(Node localRoot)
   {
   if(localRoot != null)
      {
      postOrder(localRoot.leftChild);
      postOrder(localRoot.rightChild);
      System.out.print(localRoot.cData + " ");
      }
   }
//check the whether a node is a signal
public static boolean check(Character ch){
    if(ch.equals(new Character(‘+‘)) || ch.equals(new Character(‘-‘))
            || ch.equals(new Character(‘*‘))
            || ch.equals(new Character(‘\\‘) )
                    || ch.equals(new Character(‘^‘))        )
                      {
                return true;
    }
    return false;
}
public long calculate( ){
    long result = 0;
    result+=this.root.cData-48;
    Node localRoot= root.leftChild;
    while(localRoot!=null){
            if(localRoot.cData==‘+‘){
                if(localRoot.rightChild!=null){
                    result+=localRoot.rightChild.cData-48;
                }
            }
            if(localRoot.cData==‘-‘){
                if(localRoot.rightChild!=null){
                    result-=localRoot.rightChild.cData-48;
                }
            }
            if(localRoot.cData==‘/‘){
                if(localRoot.rightChild!=null){
                    result/=localRoot.rightChild.cData-48;
                }
            }
            if(localRoot.cData==‘*‘){
                if(localRoot.rightChild!=null){
                    result*=localRoot.rightChild.cData-48;
                }
            }

             /*int m = 4;
             int n = 2;
             int result = 1;
             for(int i=0;i<n;i++){
                 result*=m;
             }
             System.out.println(result);*/

            if(localRoot.cData==‘^‘){
                long temp = 1;
                for(int i=0;i<localRoot.rightChild.cData-48;i++){
                    temp*=result;
                }
                result= temp;
            }
            localRoot= localRoot.leftChild;
    }
    return result;
}
    //-------------------------------------------------------------
public void displayTree()
   {
   Stack globalStack = new Stack();
   globalStack.push(root);
   int nBlanks = 32;
   boolean isRowEmpty = false;
   System.out.println(
   "......................................................");
   while(isRowEmpty==false)
      {
      Stack localStack = new Stack();
      isRowEmpty = true;
      for(int j=0; j<nBlanks; j++)
         System.out.print(‘ ‘);
      while(globalStack.isEmpty()==false)
         {
         Node temp = (Node)globalStack.pop();
         if(temp != null)
            {
            System.out.print(temp.cData);
            localStack.push(temp.leftChild);
            localStack.push(temp.rightChild);
            if(temp.leftChild != null ||
                                temp.rightChild != null)
               isRowEmpty = false;
            }
         else
            {
            System.out.print("--");
            localStack.push(null);
            localStack.push(null);
            }
         for(int j=0; j<nBlanks*2-2; j++)
            System.out.print(‘ ‘);
         }  // end while globalStack not empty
      System.out.println();
      nBlanks /= 2;
      while(localStack.isEmpty()==false)
         globalStack.push( localStack.pop() );
      }  // end while isRowEmpty is false
   System.out.println(
   "......................................................");
   }  // end displayTree()
//-------------------------------------------------------------
}  // end class Tree
public class TreeApp{
    public static void main(String[] args) {
        Tree tree = new Tree();
        tree.insert(‘2‘);
        tree.insert(‘^‘);
        tree.insert(‘3‘);
        tree.insert(‘+‘);
        tree.insert(‘1‘);
        tree.insert(‘*‘);
        tree.insert(‘2‘);
         tree.displayTree();
    long result = tree.calculate();
     System.out.println("----"+result+"----");
    }
}
时间: 2024-08-29 05:33:03

深圳同城快跑笔试题目 3 实现四则运算的相关文章

深圳同城快跑笔试题目 2 实现json字符串保存到本地硬盘

//从给定位置读取Json文件 public static String readJson(String path){ //从给定位置获取文件 File file = new File(path); BufferedReader reader = null; //返回值,使用StringBuffer StringBuffer data = new StringBuffer(); // try { reader = new BufferedReader(new FileReader(file));

LYK 快跑!(run)

LYK 快跑!(run)Time Limit:5000ms Memory Limit:64MB[题目描述] LYK 陷进了一个迷宫! 这个迷宫是网格图形状的. LYK 一开始在(1,1)位置, 出口在(n,m).而且这个迷宫里有很多怪兽,若第 a 行第 b 列有一个怪兽,且此时 LYK 处于第 c 行 d 列,此时这个怪兽对它的威胁程度为|a-c|+|b-d|.LYK 想找到一条路径,使得它能从(1,1)到达(n,m),且在途中对它威胁程度最小的怪兽的威胁程度尽可能大.当然若起点或者终点处有怪兽

C++笔试题目大全(笔试宝典)(不断完善中)

1.new . delete . malloc . free 关系 delete 会调用对象的析构函数 , 和 new 对应 free 只会释放内存, new 调用构造函数. malloc 与 free 是 C++/C 语言的标准库函数, new/delete 是 C++ 的运算符.它们都可用于申请动态内存和释放内存.对于非内部数据类型的对象而言,光用 maloc/free 无法满足动态对象的要求.对象在创建的同时要自动执行构造函数,对象在消亡之前要自动执行析构函数.由于 malloc/free

算法学习 并查集(笔试题目:找同伙)

题目背景太长,记得不清楚,暂参考<啊哈算法>一书,根据笔试题目大意改编如下: 警察正要捉获某地区的犯罪团伙,由于强盗人数过大,想查清楚有几个团伙非常困难. 根据上级指示,需要首先尽快抓获强盗A所在的团伙,这需要掌握 1 所在团伙的人数.先有资料如下: 强盗1 和 强盗2 是同伙 强盗3 和 强盗4 是同伙 强盗2 和 强盗5 是同伙 强盗3 和 强盗2 是同伙 注意,强盗的同伙的同伙也是同伙,问  强盗1 的同伙(不包括1自己)有多少人? 该题形式化表示如下: 每个测试实例首先包括2个整数:N

【python游戏编程之旅】第九篇---嗷大喵快跑小游戏开发实例

本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 前几期博客我们一起学习了,pygame中的冲突检测技术以及一些常用的数据结构. 这次我们来一起做一个简单的酷跑类游戏综合运用以前学到的知识. 程序下载地址:http://yunpan.cn/cLIcJgTvq4tZS 访问密码 901f 源代码网盘地址:http://yunpan.cn/cLIc67S4nNRFY 访问密码 c139 github地址:https://github.com/XINCGer/

2017 校招网上笔试题目

2017 校招网上笔试题目 做了一下某大厂的笔试的题目 1. 一个表, visit(cookie_id, area1, area2, date) 给出各种查询策略中, 哪一种最好,每一条SQL语句大同小异, 几乎看不出区别, 还有6,7个选项,同时字数很长.一般的SQL语句好像有根据查询的嵌套顺序来比较差异的,但是这道题反而没有.反正蒙了一个.没有数字可算,心里没有底,应该证明了SQL语法可能会很大地影响查询效率. 2. 给了几个条件,判断你的名次.又是球赛的问题,考查你的推理能力. 3. en

几道经典的SQL笔试题目

几道经典的SQL笔试题目(有答案) (1)表名:购物信息 购物人      商品名称     数量 A            甲          2 B            乙          4 C            丙          1 A            丁          2 B            丙          5 …… (其他用户实验的记录大家可自行插入) 给出所有购入商品为两种或两种以上的购物人记录 答:select * from 购物信息 wher

大话重构连载4:大布局与小步快跑

以往我们在重新设计一个系统时,总是喜欢大布局.全面地整理系统需求,全面地分析系统功能,再全面地设计系统.开发.测试.这样一个过程往往会持续数月,花费大量的工作量.但是,不到最后设计出来,谁都不知道会不会存在问题.这就是“大布局”的弊病. 正因为如此,软件大师在讲述系统重构时总是强调,系统重构应当避免大设计,而应当尽量采用一个一个连续不断的小设计.这就是我们所说的“小步快跑”的设计模式. 小步快跑体现出了敏捷软件开发的特点——简单与快速反馈.不要想得太多了,活在今天的格子里,因为你永远不可能预知今

分享两道笔试题目

前几天,给成都的某家公司投了个简历,给发了两道笔试题目,与大家分享一下.附上自己的解题过程,写得不好的地方,还请博友多多指教. 一 .  设计程序输出销售及收费清单 一个电商平台对在其平台之上销售的除了书籍.食品以及药物以外的商品收取 10% 的费用.而对于进口的商品则额外收取 5% 的附加费用.对于平台抽取的费用计算时,舍入的规则是:对于 n% 抽取率,价格为 p的商品, np/100 的值就近舍入到 0.05(如: 7.125 -> 7.15, 6.66 -> 6.70 ). 卖家卖出一些