Company A面试 笔试 : 爬山 算步骤 问题

来源:互联网 发布:linux和windows的优劣 编辑:程序博客网 时间:2024/04/30 15:55
/*
 somebody go from A (0,0) to B (0,D) via many mountains which use tri-tuple <startx,endx,height>.
 * this guy must be walk up  when arriving at mountains and climb down when leave this mountain.

 * Here 这里山的坐标会交叉或者重叠。

   questions: how many steps after he cross these mountains from starting point (0,0).  

 * 
 *|
 * input:
         3  //total  mountain's number
   mountain1:  <startx1,endx1,height1>
 mountain2:   <startx2,endx2,height2>

 mountain3:  <startx3,endx3,height3>


 * output:
 *     steps
 * 
 * Solution:
 *  1) 垂直方向的steps 就是 把各个合体山(几个起点或终点坐标有交差重叠的山)的最高高度乘以2倍,再把这些数值加起来。
 *      如果合体山的起始点与前面的合体山的终点是一样的,则要注意特殊处理,see 
 *  2) 水平方向的steps 就是最远点的坐标点
 */
#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <stack>
#include <list>
#include <iostream>
#include <algorithm>
#include <cstring>


//must include it for strlen() in some compilers
using namespace std;


class Node {
private:
 long PointValue;
 long height;
 bool Isstartpoint; 


 public:
     Node(long value, long height, bool Isstartpoint){
         this->PointValue=value;
         this->height=height;
         this->Isstartpoint=Isstartpoint;
     }
     virtual ~Node(){
         ;
     }
     
     long getPointValue() {
         return PointValue;
     }
     long getheight() {
         return height;
     };
     bool IsStartPoint() {
         return this->Isstartpoint;
     }
     
     bool operator <( const Node& rt) const {  
        cout<<"call this operator <"<<endl;
       return ( PointValue < rt.PointValue);  
  }  
     bool operator >( const Node& rt) const{  
         cout<<"call this operator >"<<endl;
       return (PointValue > rt.PointValue);  
   }  
};


bool isSpace(char x){
    return x == ' ' || x == '\r' || x == '\n' || x == '\r' || x == '\b' || x == '\t';
}


char * rightTrim(char *str){
      long len = strlen(str);
    while(--len>=0){
        if(isSpace(str[len])){
            str[len] = '\0';
        }else{
            break;
        }
    }
    return str;
}


char * getInputLine(char *buffer,   long length){
    if(fgets(buffer,length, stdin)==NULL){
        return NULL;
    }
    rightTrim(buffer);
    if(strlen(buffer)<=0){
        return NULL;
    }
    return buffer;
}


//array saves data delimited by ',' parsed from one line.
//return the total number of data
long splitAndConvert(char* strings, long *array){
    char* tokenPtr = strtok(strings,",");
      long i=0;
    while(tokenPtr != NULL){
        array[i] = atol(tokenPtr);
        i++;
        tokenPtr=strtok(NULL,",");
    }
    return i;
}
  /*
      | ------ |
   ------------------    
   |                 |
 -----------------------
   */
  long process_data(vector<Node>& datavector){
    
      //sort data point increasingly
     sort(datavector.begin(),datavector.end(),less<Node>());
     //sort data point decreasingly
     //sort(datavector.begin(),datavector.end(),greater<Node>());
    
     long steps=0;
     long current_height=0;
     long lastMountheight=0;
     bool mountainconnected=false;
     
     int accumulate=0; //用于找到合体山,即起始点和终止点个数一样。
     
     // 垂直方向的steps 就是把各个合体山的高度的2倍加起来。
     
     for(int len=0; len <datavector.size();len++ ){
         if(datavector[len].IsStartPoint()) 
             accumulate++;
         else 
             accumulate--;
         
         //find the highest height of this complicated mountain
         if(datavector[len].getheight() > current_height)
             current_height=datavector[len].getheight();
                          
         //find the complete complicated mountain
         if(accumulate==0) {
             //if this mountain is connected to former mountain
             if(mountainconnected==true){
                 if(lastMountheight < current_height) {
                     steps += 2*(current_height - lastMountheight);
                 } 
                 //if lastMountheight  > current, nothing to do
             } 
             else {
                 steps += (2*current_height);
             }
             
            lastMountheight=current_height;
            current_height=0;   
             
            //check whether this mountain is connected the next mountain, that is,its endpoint is the same as the start point of next mountain.
            if(len <(datavector.size()-1)) {
                if(datavector[len].getPointValue()==datavector[len+1].getPointValue()){
                    mountainconnected=true;
                }
                else  {
                    mountainconnected=false; 
                }
            }
         }
     }
     //水平方向的steps 就是最远点的坐标点
     //datavector.front()to get this first element;
     steps += datavector.back().getPointValue(); //get this furthest point value
     return steps;
  }
  
int main(int argc, char** argv) {
    char *line=new char[1000];
    cout<<"input number of mountains:"<<endl;
    getInputLine(line,1000);
    long number[1];
    long NumberLength = splitAndConvert(line,number);
  
    if(NumberLength != 1) {
        cout<<"error"<<endl;
        return 0;
    }
    cout<<"input mountains <start,end,height>"<<endl;;
    //save all input int data one by one
    vector<Node> datavector;
    int index=0;
    while(index<number[0]){
        //get trip tuple for each mountain data from stdin
         getInputLine(line, 1000); 
         long data[3];
         long len=splitAndConvert(line,data);
       //valid check for array
       if(len !=3 ){
         cout<<" input parameter error, exit "<<endl;
         return 0;   
        }  
      index++;
      
      Node mnode1(data[0],data[2],true); //start point
      datavector.push_back(mnode1);
      Node mnode2(data[1],data[2],false); //end point
      datavector.push_back(mnode2);
    }
    cout<<process_data(datavector)<<endl;
    cout <<"completes successfully, exit"<<endl;         
    return 0;
}
0 0
原创粉丝点击