sicily考试模拟题-1000超级和 1001会议室安排 1002二叉树比较

1000


题目描述

Description
定义超级和函数F如下:

F(0, n) = n,对于所有的正整数n..
F(k, n) = F(k – 1, 1) + F(k – 1, 2) + … + F(k – 1, n),对于所有的正整数k和n.

请实现下面Solution类中计算F(k, n)的函数(1 <= k, n <= 14).

class Solution {
public:
       int F(int k, int n) {

       }
};

例1F(1, 3) = 62F(2, 3) = 103F(10, 10) = 167960

注意:你只需要提交Solution类的代码,你在本地可以编写main函数测试程序,但不需要提交main函数的代码. 注意不要修改类和函数的名称.

解题代码

#include <iostream>
using namespace std;


class Solution 
{
public:

  int F(int k,int n){
    if(k==0) return n;
    else{
      int res=0;
      for (int i = 1; i <= n; ++i)
        res+=F(k-1,i);
      return res;
    }
  }


};


int main(int argc, char const *argv[])
{
  Solution sol;
  cout<<"solution is :"<<sol.F(1,3)<<endl;
  cout<<"solution is :"<<sol.F(2,3)<<endl;
  cout<<"solution is :"<<sol.F(10,10)<<endl;

  /* code */
  return 0;
}

1001会议室安排


题目描述

N个会议要同时举行,参会人数分别为A[0], A[1], ..., A[N-1]. 现有M个会议室,会议室可容纳人数分别为B[0], B[1], ..., B[M-1]. 当A[i]<=B[j]时,可以把会议i安排在会议室j,每间会议室最多安排一个会议,每个会议最多只能安排一个会议室. 求最多安排多少个会议.

1 <= N, M <= 100000, 每个会议的参会人数和每间会议室的容纳人数均在11000之间.

请为下面的Solution类实现解决上述问题的函数assignConferenceRoom. 函数参数A和B的意义如上,返回值为最多可安排的会议数.

class Solution {
public:
    int assignConferenceRoom(vector<int>& A, vector<int>& B) {

    }
};

例1:A={2, 3}, B={1, 2},答案为1.2:A={3, 4, 5},B={10, 3, 2},答案为2.

注意:你只需要提交Solution类的代码,你在本地可以编写main函数测试程序,但不需要提交main函数的代码. 注意不要修改类和函数的名称.

解题思路

先排序,然后从最大的人数和最大的会议室开始入手考虑,贪心算法。

解题代码

//1001-会议室安排
#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;


class Solution 
{
public:

  int assignConferenceRoom(vector<int>& A, vector<int>& B){
    int res=0;
    // bool decrease(const int &a,const int &b){
    //  return a>b;
    // }

    // bool decrease(pair<string,int>& a,pair<string,int>& b){
    //  return a.second>b.second;
    // }

    sort(A.begin(),A.end());
    sort(B.begin(),B.end());

    while(A.size() && B.size() )
    {
      //参会人数比最大的会议室还多
      if(A[A.size()]>B[B.size()])
        A.pop_back();
      else{
        A.pop_back();
        B.pop_back();
        res++;
      }
    }

    return res; 
  } 
};


int main(int argc, char const *argv[])
{
  Solution sol;

  cout<<"-------------------------------------------"<<endl;
  int arr1[]={3,4,5}; int arr2[]={10,3,2}; 
  vector<int>A(begin(arr1),end(arr1));
  vector<int>B(begin(arr2),end(arr2));
  cout<<"solution is :"<<sol.assignConferenceRoom(A,B)<<endl;
  // cout<<"solution is :"<<sol.F(2,3)<<endl;
  // cout<<"solution is :"<<sol.F(10,10)<<endl;

  cout<<"-------------------------------------------"<<endl;
  return 0;
}

1002二叉树比较


题目描述

两个二叉树结构相同,且对应结点的值相同,我们称这两个二叉树等价.

例如:以下两个二叉树等价
        1           1
       /  \         /  \
      2   3       2   3
而以下两个则不等价
        1           1
       /  \         /  \
      2   3       3   2
以下两个也不等价
        1           1
       /  \         /  \
      2   3       2   2

给出两个二叉树p和q,判断它们是否等价.

p和q的结点数不多于100000,每个结点的数值在1和1000000000之间.

请为下面的Solution类实现解决上述问题的isEqual函数,函数的两个参数p和q分别代表两个二叉树的根节点,如果以p和q为根的二叉树等价则函数返回true,否则返回false.

解题思路

递归的思想,递归比较当前节点值,递归的对象是当前节点的左右子树。

解题代码

#include <iostream>
#include <vector>
using namespace std;

/**
  Definition for a binary tree node.
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };
 */


class TreeNode {
public:
      int val;
      TreeNode *left;
      TreeNode *right;

      TreeNode(int x){
        val = x;
        left = NULL;
        right= NULL;
      }
};


class Solution {
public:
       bool isEqual(TreeNode* p, TreeNode* q) {
           //p q为空的情况
           if ( p==NULL && q==NULL ){
             return true;
           }
           //一个为空 另外一个不为空
           else if ( !(p&&q) ){
             return false;
           }

           else{
              if(p->val!=q->val)  return false;
              else 
                return isEqual(p->left,q->left)&&
                        isEqual(p->right,q->right);
           }
        }

};



int main()
{
   TreeNode *p=new TreeNode(1);
   TreeNode *q=new TreeNode(1);

   p->left=new TreeNode(2);p->right=new TreeNode(3);
   q->left=new TreeNode(2);q->right=new TreeNode(2);

   Solution sol;
   if(sol.isEqual(p,q))
      cout<<"equal!\n";
   else
      cout<<"not equal!\n";

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值