机试指南第十章 数据结构二

P164 习题10.1

http://t.cn/Ai9PUJtK
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

string str;

struct TreeNode{
    int data;
    TreeNode* leftchild;
    TreeNode* rightchild;
    TreeNode(int x):data(x),leftchild(NULL),rightchild(NULL){}
};

TreeNode* Insert(TreeNode* root,int x){
    if (root == NULL){
        root = new TreeNode(x);
    }
    else if (x<root->data){
        root->leftchild = Insert(root->leftchild,x);
    }
    else if (x>root->data){
        root->rightchild = Insert(root->rightchild,x);
    }
    return root;
}

void preorder(TreeNode* root){
    if (root == NULL){return;}
    str += root->data-'0';
    preorder(root->leftchild);
    preorder(root->rightchild);
}

void inorder(TreeNode* root){
    if (root == NULL){return;}
    inorder(root->leftchild);
    str += root->data-'0';
    inorder(root->rightchild);
}


int main(){
    //freopen("D://case.txt","r",stdin);
    int n;
    cin>>n;
    string father;
    cin>>father;
    TreeNode* rootfather = NULL;
    for (int i=0;i<father.size();i++){
        rootfather = Insert(rootfather,father[i]-'0');
    }
    str = "";
    preorder(rootfather);
    string pre = str;
    str = "";
    inorder(rootfather);
    string in = str;
    while(n--){
        string son;
        cin>>son;
        TreeNode* rootson = NULL;
        for (int i=0;i<son.size();i++){
            rootson = Insert(rootson,son[i]-'0');
        }
        str = "";
        preorder(rootson);
        string prestr = str;
        str = "";
        inorder(rootson);
        string inpre = str;
        if (prestr == pre && inpre == in){cout<<"YES"<<endl;}
        else{cout<<"NO"<<endl;}
    }
    cin>>n;
}j

P169 习题10.2

http://t.cn/AiCu5hcK
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

int main(){
    //freopen("D://case.txt","r",stdin);
    int n;
    cin>>n;
    int temp;
    priority_queue <int,vector <int>,greater <int> > prique;
    while(n--){
        cin>>temp;
        prique.push(temp);
    }
    int k;
    cin>>k;
    int counter = 0;
    int p = 1001;
    while(!prique.empty()){
        if (p!=prique.top()){
            counter++;
        }
        p = prique.top();
        prique.pop();
        if (counter == k){cout<<p;break;}
    }
}

P170 习题10.3

http://t.cn/AiCu5nsQ
在这里插入图片描述

P178 习题10.4

http://t.cn/AiCuM7nj
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <string>

using namespace std;

int main(){
    //freopen("D://case.txt","r",stdin);
    map <int,int> m;
    int n;
    while(cin>>n){
        if (n == 0){break;}
        while(n--){
            int score;
            cin>>score;
            if (m.find(score)!=m.end()){m[score]++;}
            else{m[score] = 1;}
        }
        int destination;
        cin>>destination;
        if (m.find(destination)!=m.end()){cout<<m[destination]<<endl;continue;}
        cout<<'0'<<endl;
    }
}

P178 习题10.5

http://t.cn/AiCuM09f
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <string>

using namespace std;

struct personarrive{
    string arrive;
    string left;
    //重载小于等于号,使得map可以排大小
    bool operator <(const personarrive& x) const{
        if(arrive<x.arrive){
            return true;
        }
        return false;
    }
};

struct personleft{
    string arrive;
    string left;
    //重载小于等于号,使得map可以排大小
    bool operator <(const personleft& x) const{
        if(left>x.left){
            return true;
        }
        return false;
    }
};

bool comparearrive(){return true;}

int main(){
    //freopen("D://case.txt","r",stdin);
    map <personarrive,string> marrive;
    map <personleft,string> mleft;
    int casenum;
    cin>>casenum;
    while(casenum--){
        personarrive p;
        personleft q;
        string id;
        cin>>id;
        cin>>p.arrive;
        cin>>p.left;
        q.arrive = p.arrive;
        q.left = p.left;
        marrive[p] = id;
        mleft[q] = id;
    }
    map <personarrive,string>::iterator itarrive;
    map <personleft,string>::iterator itleft;
    itarrive = marrive.begin();
    itleft = mleft.begin();
    cout<<itarrive->second<<' ';
    cout<<itleft->second;
}

P178 习题10.6

http://t.cn/AiCux4f7
在这里插入图片描述

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main(){
    freopen("D://case.txt","r",stdin);
    int n,m;
    while(cin>>n>>m){
        vector<int> vec;
        map<int,int> ma;
        int temp;
        while (n--){
            cin>>temp;
            ma[temp]++;
            vec.push_back(temp);
        }
        for(int i=0;i<vec.size();i++){
            int t = ma[vec[i]];
            if(t==1)
                cout<<"BeiJu"<<endl;
            else
                cout<<t-1<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值