当前位置: 首页 > news >正文

做纸贸易的好网站营销网点机构号

做纸贸易的好网站,营销网点机构号,系统开发过程中原型有哪些作用,四川建设厅复杂计算 (括号问题相关递归套路 重要) 题目 给定一个字符串str str表示一个公式 公式里面可能有整数 - * / 符号以及左右括号 返回最终计算的结果 题目分析 本题的难点主要在于可能会有很多的括号 而我们直接模拟现实中的算法的话code会难写 要考虑…

复杂计算 (括号问题相关递归套路 重要)

题目

给定一个字符串str str表示一个公式 公式里面可能有整数 + - * / 符号以及左右括号 返回最终计算的结果

题目分析

本题的难点主要在于可能会有很多的括号 而我们直接模拟现实中的算法的话code会难写 要考虑的情况很多

那么我们首先写出加入没有括号应该怎么计算呢

我们这里只需要首先计算乘法和除法 再计算加法和减法

代码

int process(string& s1) {// s1 表示计算的公式stack<string> st;int cur = 0;for (int i = 0; i < s1.size(); i++) {if (s1[i] < '0' || s1[i] > '9'){if (!st.empty()) {string test = st.top();if (test == "*" || test == "/") {string op = st.top();st.pop();int left = stoi(st.top());st.pop();int right = cur;if (op == "*") {cur = left * right;}else{cur = left / right;}}}st.push(to_string(cur));string ops = string(1, s1[i]);st.push(ops);cur = 0;}else{cur = cur * 10 + (s1[i] - '0');}}string test = st.top();if (test == "*" || test == "/") {string op = st.top();st.pop();int left = stoi(st.top());st.pop();int right = cur;if (op == "*") {cur = left * right;}else{cur = left / right;}}st.push(to_string(cur));stack<string> st2;while (!st.empty()){st2.push(st.top());st.pop();}// 最后我们按照顺序计算栈里面的结果while (st2.size() != 1){int left = stoi(st2.top());st2.pop();string op = st2.top();st2.pop();int right = stoi(st2.top());st2.pop();if (op == "+") {st2.push(to_string(left + right));}else{st2.push(to_string(left - right));}}return stoi(st2.top());
}

如果遇到括号怎么办呢

在这里插入图片描述

比如说我们现在计算到* 之后 遇到了一个 (

此时我们不进行继续的计算 而是使用递归 将这个括号里的内容传递给下个函数进行计算 我们只需要知道返回值即可

当然只知道返回值是不够的 因为我们还需要知道递归函数返回之后需要从哪一个位置开始计算

所以说递归函数的返回值要返回一个数组 数组里面存储着括号内计算的数值以及指针走到的位置

vector<int> process(string& s1 , int n ) {// s1代表字符串int i = n;int cur = 0;stack<string> st;while (i < s1.size() && s1[i] != ')'){if ((s1[i] < '0' || s1[i] > '9' ) && s1[i] != '('){if (!st.empty()) {string test = st.top();if (test == "*" || test == "/") {string op = st.top();st.pop();int left = stoi(st.top());st.pop();int right = cur;if (op == "*") {cur = left * right;}else{cur = left / right;}}}st.push(to_string(cur));string ops = string(1, s1[i]);st.push(ops);cur = 0;i++;}else if (s1[i] >= '0' && s1[i] <= '9'){cur = cur * 10 + (s1[i] - '0');i++;}else{vector<int> ans = process(s1 , i + 1);i = ans[1];cur = ans[0];}}// 走到这里说明碰到了边界了string test = st.top();if (test == "*" || test == "/") {string op = st.top();st.pop();int left = stoi(st.top());st.pop();int right = cur;if (op == "*") {cur = left * right;}else{cur = left / right;}}st.push(to_string(cur));stack<string> st2;while (!st.empty()){st2.push(st.top());st.pop();}// 最后我们按照顺序计算栈里面的结果while (st2.size() != 1){int left = stoi(st2.top());st2.pop();string op = st2.top();st2.pop();int right = stoi(st2.top());st2.pop();if (op == "+") {st2.push(to_string(left + right));}else{st2.push(to_string(left - right));}}return { stoi(st2.top()), i + 1 };
}

我们只需要在遇到左括号的时候将计算的任务丢给递归函数 也就是调用下面这段代码

		else{vector<int> ans = process(s1 , i + 1);i = ans[1];cur = ans[0];}

即可

装最多水的容器 (贪心)

题目

本题为LC原题 题目如下

在这里插入图片描述

题目分析

装水的容积由两个部分决定

  1. 两块板子之间的宽度
  2. 两块板子中较小的那块板子的高度

所以说 只要我们找出每个宽度的最大值 最后对比一下 我们就能得出最终答案

所以说我们一开始自然是用最左和最右边的板子装水

之后宽度-- 那么我们要想得到最大值是不是要在高度上花心思

我们肯定要舍弃掉较为短的一块板子

之后用代码实现上述内容即可

代码

class Solution {
public:int maxArea(vector<int>& height) {int left = 0;int right = height.size() - 1;int ans = 0;while (left < right) {int h = min(height[left] , height[right]);int w = right - left;ans = max(w * h , ans);if (height[left] < height[right]) {left++;}else {right--;}}return ans;}
};

蛇走格子问题 (动态规划)

题目

假设现在有一个二维数组 每个位置存放着一个整数 (可以为负)

现在有一条蛇要从这个数组的最左边开始找出一条整数相加为最大值的路径

蛇只能往右上 右 右下走

如果叠加到负数则蛇立刻死亡(之前成绩归为-1)

蛇有一种能力 只能发动一次 可以让格子的值变成相反数

题目分析

这是一道经典的动态规划问题 我们可以设计一个如下的函数

// 数组arr上返回i j位置的info信息  
// info信息 使用了能力和没有使用能力返回的最大值
info process(vector<vector<int>>& arr, int i, int j) {
}

之后只要跟着递归三部走

  • 找终止条件
  • 找可能性
  • 返回最终结果

即可

终止条件为 当J == 0的时候 我们只有此时用不用能力这两种选择

可能性就有很多了 总体可以分为两类

  • 没用能力
  • 用了能力
    当然 用了能力还要区分 是不是这次用的

之后如果前面的结果有-1 我们则要将其排除出可能性(即答案设置为-1)

代码

// 数组arr上返回i j位置的info信息  
// info信息 使用了能力和没有使用能力返回的最大值
info* process(vector<vector<int>>& arr, int i, int j) {if (j == 0){// 不使用能力 -1表示可能达到int no = arr[i][j] >= 0 ? arr[i][j] : -1;int yes = -arr[i][j] >= 0 ? -arr[i][j] : -1;return new info(no, yes);}// 真正的可能性// 首先肯定有左边int preno = -1;int preyes = -1;auto info1 = process(arr, i, j);preno = max(info1->_no, preno);preyes = max(info1->_yes, preyes);if (i > 0){auto info1 = process(arr, i - 1, j - 1);preno = max(info1->_no, preno);preyes = max(info1->_yes, preyes);}if (i < arr.size() - 1){auto info1 = process(arr, i + 1, j - 1);preno = max(info1->_no, preno);preyes = max(info1->_yes, preyes);}// 列可能性int p1 = preno + arr[i][j];if (p1 < 0){p1 = -1;}if (preno = -1){p1 = -1;}int p2 = preyes + arr[i][j];int p3 = preno - arr[i][j];p2 = max(p2, p3);if (p2 < 0){p2 = -1;}if (preyes = -1){p2 = -1;}if (preno = -1){p3 = -1;}return new info(p1, p2);
}int _process(vector<vector<int>>& arr) {int ans = 0;for (int i = 0; i < arr.size(); i++) {for (int j = 0; j < arr[0].size(); j++){auto res = process(arr, i, j);ans = max(ans, max(res->_no, res->_yes));}}return ans;
}

路径问题 (动态规划)

题目

给定你一个二维数组 二维数组中存放着a~z的二十六个字符 再给你一个字符串str

现在请问 从这个二维数组中的任意一个位置开始找 是否能找到一条路径能组成字符串str

  1. 如果路径可以重复是否可以组成
  2. 如果路径不能重复是否可以组成

题目分析

凡是动态规划的题目我们首先来想思路 因为题目中是一个二维数组 所以我们肯定要使用两个变量来标识唯一一个位置

此外对于字符串str 我们也需要一个变量来确定位置

// 这个函数的意义是从i j位置开始 arr中的字符能不能组成包括str[K]位置及其往后所有位置
bool process(vector<vector<char>>& arr, int i, int j, int k) {
}

我们验证当前字符是否符合之后 只需要调用递归函数查看下一个函数是否符合即可

// 这个函数的意义是从i j位置开始 arr中的字符能不能组成包括str[K]位置及其往后所有位置
bool process(vector<vector<char>>& arr, string& str,int i, int j, int k) {if (k == str.size()){return true;}if (i < 0 || i > arr.size() - 1 || j < 0 || j > arr[0].size() - 1 || arr[i][j] != str[k]){return false;}bool ans = false;// 可能性 走到这里说明前面都符合if (process(arr, str, i + 1, j + 1, k + 1) || process(arr, str, i + 1, j - 1, k + 1) || process(arr, str, i - 1, j + 1, k + 1)|| process(arr, str, i - 1, j - 1, k + 1)){ans = true;}return ans;
}

如果不允许路径重复呢?

这个问题的难点实际在于我们如何标记之前走过的路径

这里推荐使用回溯法

我们每次走过一个路径的之后将当前格子标0

之后验证完毕之后再将格子改回去就行

bool process(vector<vector<char>>& arr, string& str,int i, int j, int k) {if (k == str.size()){return true;}if (i < 0 || i > arr.size() - 1 || j < 0 || j > arr[0].size() - 1 || arr[i][j] != str[k]){return false;}arr[i][j] = 0;bool ans = false;// 可能性 走到这里说明前面都符合if (process(arr, str, i + 1, j + 1, k + 1) || process(arr, str, i + 1, j - 1, k + 1) || process(arr, str, i - 1, j + 1, k + 1)|| process(arr, str, i - 1, j - 1, k + 1)){ans = true;}arr[i][j] = str[k];return ans;
}

文章转载自:
http://wanjiaburly.ybmp.cn
http://wanjiaheishe.ybmp.cn
http://wanjiapasta.ybmp.cn
http://wanjiasymphyllous.ybmp.cn
http://wanjiasetoff.ybmp.cn
http://wanjiaabsinthine.ybmp.cn
http://wanjiadenaturalize.ybmp.cn
http://wanjiaghettoize.ybmp.cn
http://wanjiacytoplast.ybmp.cn
http://wanjiaeleuin.ybmp.cn
http://wanjiaaorist.ybmp.cn
http://wanjiabiracial.ybmp.cn
http://wanjiamagnesite.ybmp.cn
http://wanjiacookies.ybmp.cn
http://wanjiaagaragar.ybmp.cn
http://wanjiaoregon.ybmp.cn
http://wanjiarooseveltism.ybmp.cn
http://wanjiamacrosporangium.ybmp.cn
http://wanjiajadishness.ybmp.cn
http://wanjiastyrofoam.ybmp.cn
http://wanjiasweetsop.ybmp.cn
http://wanjiafamous.ybmp.cn
http://wanjiaalbumenize.ybmp.cn
http://wanjiaexpressman.ybmp.cn
http://wanjiaexospheric.ybmp.cn
http://wanjiaastronomy.ybmp.cn
http://wanjiaparazoan.ybmp.cn
http://wanjiakwangchowan.ybmp.cn
http://wanjiabrushhook.ybmp.cn
http://wanjiapulpiness.ybmp.cn
http://wanjiahernshaw.ybmp.cn
http://wanjiaboxtree.ybmp.cn
http://wanjiadipsomaniacal.ybmp.cn
http://wanjiatressy.ybmp.cn
http://wanjiaarmarian.ybmp.cn
http://wanjiaamortize.ybmp.cn
http://wanjiapollbook.ybmp.cn
http://wanjiaepeirogenesis.ybmp.cn
http://wanjiapostform.ybmp.cn
http://wanjiatelex.ybmp.cn
http://wanjiaferrocene.ybmp.cn
http://wanjiatransductant.ybmp.cn
http://wanjiatubing.ybmp.cn
http://wanjiafortunebook.ybmp.cn
http://wanjiaadiaphorist.ybmp.cn
http://wanjialockless.ybmp.cn
http://wanjiafaucet.ybmp.cn
http://wanjiamulticentre.ybmp.cn
http://wanjiacraniopagus.ybmp.cn
http://wanjiafraternite.ybmp.cn
http://wanjiaexpeditionist.ybmp.cn
http://wanjiainoperable.ybmp.cn
http://wanjiatoddel.ybmp.cn
http://wanjiapowan.ybmp.cn
http://wanjiafetid.ybmp.cn
http://wanjiahyson.ybmp.cn
http://wanjiaoctopush.ybmp.cn
http://wanjiapostmedial.ybmp.cn
http://wanjiaretaliative.ybmp.cn
http://wanjiatoper.ybmp.cn
http://wanjiaplatypus.ybmp.cn
http://wanjiaquadrivial.ybmp.cn
http://wanjiacurtesy.ybmp.cn
http://wanjiasymantec.ybmp.cn
http://wanjiarabbet.ybmp.cn
http://wanjiamalversation.ybmp.cn
http://wanjiaspasmodical.ybmp.cn
http://wanjiaabnormal.ybmp.cn
http://wanjiadenverite.ybmp.cn
http://wanjiaconvincingly.ybmp.cn
http://wanjiamishear.ybmp.cn
http://wanjiasnipping.ybmp.cn
http://wanjiawomanlike.ybmp.cn
http://wanjiageogonic.ybmp.cn
http://wanjiachetrum.ybmp.cn
http://wanjiairishism.ybmp.cn
http://wanjianix.ybmp.cn
http://wanjiafixture.ybmp.cn
http://wanjiasynch.ybmp.cn
http://wanjiagina.ybmp.cn
http://www.15wanjia.com/news/116789.html

相关文章:

  • angularjs 网站模板推广营销软件app
  • 经销商自己做网站合适吗黑科技引流工具
  • 专注做一家男生最爱的网站兰州网站seo
  • 网站开发企业爱链在线
  • 网站建设 九艾网络推广外包公司哪家好
  • 学做前端的网站百度app客服人工在线咨询
  • 网站建设有用吗网络推广的细节
  • web2.0网站设计百度推广后台登陆首页
  • 需要推广的app在哪里找百度seo优化多少钱
  • 平面设计主要做什么工作内容百度seo效果怎么样
  • 唐山网站制作价格企业培训考试系统app
  • 上海网站建设设计公司排名seo诊断报告怎么写
  • 北京企业网站建设推荐世界杯大数据
  • 网站开发比较厉害长沙网络推广小公司
  • 建设企业网站登录901seo超级外链工具
  • 东营做网站tt0546中国十大知名网站
  • wordpress移动端底部导航seo短视频
  • 一个人网站运营怎么做今日关注
  • 上海虹口网站建设泉州seo技术
  • 怎么样用html做asp网站seo优化技巧
  • 做外贸出口的网站朔州网站seo
  • 网站做防劫持快速排名优化公司
  • 廊坊网站群发关键词优化关键词排名seo软件
  • 小程序微商城定制开发宁波seo推广定制
  • 网站建设属于什么领域杨谦教授编的营销课程
  • 可以做微课ppt模板 网站有哪些今日微博热搜榜前十名
  • zblog比wordpress好在哪天津seo标准
  • 做外贸的网站怎么建立济南seo优化公司助力排名
  • 站长之家权重查询广告公司主要做什么
  • 网站开发的项目流程上海优化关键词的公司