Sicily期末算法模拟题

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) {

       }
};

例1:F(1, 3) = 6

例2:F(2, 3) = 10

例3:F(10, 10) = 167960

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

代码:

// Problem#: 20616
// Submission#: 5133502
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
class Solution {
public:
       int F(int k, int n) {
             if(k == 0)
                 return n;
             else
             {
                 int sum = 0;
                 for(int i = 1; i <= n; i++)
                 {
                     sum += F(k-1,i);
                 }
                 return sum;
             }
       }
};                                 

1001 会议安排

题目描述:

Description
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, 每个会议的参会人数和每间会议室的容纳人数均在1和1000之间.

请为下面的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函数的代码. 注意不要修改类和函数的名称.

代码:

// Problem#: 20617
// Submission#: 5134641
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
class Solution {
public:
    int assignConferenceRoom(vector<int>& A, vector<int>& B) {
         sort(A.begin(), A.end());
         sort(B.begin(), B.end());

         //vector<int> flag(B.size(), 1);
         int count = 0;
         for(int i = A.size()-1; i >= 0; i--)
         {
             for(int j = B.size()-1; j >= 0; j--)
             {
                   if(A[i] <= B[j])
                   {
                       count++;
                       B.erase(B.begin()+j);
                       //flag[j] = 0;
                       break;
                   }  
                   if(A[i] > B[j])
                       break;    
             }

         }
         return count;

    }
};                                 

1002 等价二叉树

题目描述:

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

例如:以下两个二叉树等价
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.

/**
  Definition for a binary tree node.
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };
 */
class Solution {
public:
       bool isEqual(TreeNode* p, TreeNode* q) {

    }
};

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

代码:

// Problem#: 20618
// Submission#: 5137890
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
class Solution
{
    public:
    bool isEqual(TreeNode* p, TreeNode* q)
    {

        queue <TreeNode *> que1, que2;

        if((p == NULL && q != NULL) || (p != NULL && q == NULL))
            return false;
        else
        {
            que1.push(p);
            que2.push(q);
        }

        bool flag = true;

        while(!que1.empty() && !que2.empty())
        {
            if(que1.front() != NULL && que2.front() != NULL)
            {
                if(que1.front()->val != que2.front()->val)
                {
                    flag = false;
                    break;
                }
                else
                {
                    que1.push(que1.front()->left);
                    que1.push(que1.front()->right);
                    que1.pop();

                    que2.push(que2.front()->left);
                    que2.push(que2.front()->right);
                    que2.pop();
                }
            }
            else if((que1.front() == NULL && que2.front() != NULL) || (que1.front() != NULL && que2.front() == NULL))
            {
                flag = false;
                break;
            }
            else
            {    que1.pop();
                que2.pop();
                continue;
            }
        }

        if(que1.empty() && que2.empty() && flag)
            return true;
        else
            return false;
    }
};                                 

1003 相连的1

题目描述:

Description
对于一个01矩阵A,求其中有多少片连成一片的1. 每个1可以和上下左右的1相连.

请为下面的Solution类实现解决这一问题的函数countConnectedOnes,函数参数A为给出的01矩阵,A的行数和列数均不大于1000. 函数的返回值是问题的答案.

class Solution {
public:
    int countConnectedOnes(vector<vector<char>>& A) {

    }
};

例1:
A=
100
010
001
答案为3.

例2:
A=
1101
0101
1110
答案为2.

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

代码如下:

// Problem#: 20619
// Submission#: 5143090
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
// Problem#: 20619
// Submission#: 5143081
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
// Problem#: 20619
// Submission#: 5137957
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
class Solution {

public:

    int countConnectedOnes(vector<vector<char>>& A) {

          int i, j;
          int m = A.size();
          if(m == 0) return 0;
          int n;
          n = A[0].size();

          if(n == 0) return 0;

          vector<vector<bool>> B(m, vector<bool>(n, false));

          int count = 0;
          for(i = 0; i < m; i++)
          {
                for(j = 0; j < n; j++)
                {
                    if(A[i][j] == '1' && B[i][j] == false)
                    {
                        count++;
                        BFS(A,B,i,j);
                    }
                }
          }              
          return count;
    }

        void BFS(vector<vector<char>>& A, vector<vector<bool>>& B, int i, int j) {
        queue<pair<int, int>> que;
        que.push(make_pair(i, j));
        while (!que.empty()) {
            pair<int, int> p = que.front();
            que.pop();
            if (p.first -1  >= 0  && A[p.first -1][p.second] == '1'&&B[p.first - 1][p.second] == false) {
                que.push(make_pair(p.first - 1, p.second));
                B[p.first - 1][p.second] = true;
            }
            if (p.first + 1 <A.size() && A[p.first + 1][p.second] == '1'&&B[p.first + 1][p.second] == false) {
                que.push(make_pair(p.first + 1, p.second));
                B[p.first + 1][p.second] = true;
            }
            if (p.second - 1 >= 0 && A[p.first][p.second-1] == '1'&&B[p.first][p.second-1] == false) {
                que.push(make_pair(p.first, p.second-1));
                B[p.first][p.second-1] = true;
            }
            if (p.second + 1 < A[0].size() && A[p.first][p.second + 1] == '1'&&B[p.first][p.second + 1] == false) {
                que.push(make_pair(p.first, p.second + 1));
                B[p.first][p.second + 1] = true;
            }
        }
    }


};                                 

1004 无环图

题目描述:

Description

在图论中,如果一个有向图从任意顶点出发无法经过若干条边回到该点,则这个图是一个有向无环图(Directed Acyclic Graph,DAG). 对于一个n个节点的有向图(节点编号从0到n-1),请判断其是否为有向无环图.

图的节点数和边数均不多于100000.

请为下面的Solution类实现解决上述问题的isDAG函数,函数参数中n为图的节点数,edges是边集,edges[i]表示第i条边从edges[i].first指向edge[i].second. 如果是有向无环图返回true,否则返回false.

class Solution {
public:
       bool isDAG(int n, vector<pair<int, int>>& edges) {

    }
};

例1:
n = 3,edges = {(0, 1), (0, 2)},函数应返回true.

例2:
n = 2,edges = {(0, 1), (1, 0)},函数应返回false.

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

代码:(copy from http://blog.csdn.net/u013547284/article/details/73467415

// Problem#: 20620
// Submission#: 5145766
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
class Solution {
public:
       bool isDAG(int n, vector<pair<int, int>>& edges) {
          int num = n;
          int point[100000]={0};
          queue<int> queue1;
          vector<vector<int>> vector1;
          for(int i=0; i<n; i++){
              vector<int>* vector2 = new vector<int>();
              vector1.push_back(*vector2);
          }
          //记录每个点的入度
          for(int i=0; i<edges.size(); i++){
              pair<int, int> line = edges[i];
              int start = line.first;
              int end = line.second;
              point[end]++;
              vector1[start].push_back(end);
          }
          //查找入度为零的点
          for(int i=0; i<n; i++){
              if(point[i] == 0){
                  queue1.push(i);
                  num--;
                  point[i] = -2;
              }
          }
          while(!queue1.empty()){
              int temp = queue1.front();
              queue1.pop();
              vector<int> vector3 = vector1[temp];
              //删除与点temp为后继的点的入度
              for(int i=0; i<vector3.size(); i++){
                  int temp1 = vector3[i];
                  if(point[temp1] != -2){
                      point[temp1]--;
                      if(point[temp1] == 0){
                          num--;
                          queue1.push(temp1);
                          point[temp] = -2;
                      }
                  }
              }
          }
          if(num == 0) 
              return true;
          else 
              return false;
       }
};                                 

1005 最大和

Description
从数列A[0], A[1], A[2], …, A[N-1]中选若干个数,要求相邻的数不能都选,也就是说如果选了A[i], 就不能选A[i-1]和A[i+1]. 求能选出的最大和.

1 <= N <= 100000, 1 <= A[i] <= 1000

请为下面的Solution类实现解决上述问题的函数maxSum,函数参数A是给出的数列,返回值为所求的最大和.

class Solution {
public:
int maxSum(vector& A) {

}

};

例1:A = {2, 5, 2},答案为5.

例2:A = {2, 5, 4},答案为6.

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

代码如下:

class Solution {
public:
    int maxSum(vector<int>& A) {
        int sum[100000];
        if(A.size() == 0)
            return 0;
        else if(A.size() == 1)
            return A[0];
        else if(A.size() == 2)
            return (A[0]>A[1]?A[0]:A[1]);

        sum[0] = A[0];
        sum[1] = (A[0]>A[1]?A[0]:A[1]);;

        for(int i = 2; i < A.size(); i++)
        {
            sum[i] = (sum[i-2]+A[i])>sum[i-1]?(sum[i-2]+A[i]):sum[i-1];    
        }

        return sum[A.size()-1];
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值