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

口碑好的购物网站建设焊工培训班

口碑好的购物网站建设,焊工培训班,网站内部资源推广案例,做网站完整过程1、题目描述(全排列) 输入一个正整数n,输出1~n的全排列。 输入格式 一个正整数n。 输出格式 所有1~n的全排列,每个排列占一行。 样例输入 3 样例输出 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 算法思路 题目要求输出n的全…

1、题目描述(全排列)

输入一个正整数n,输出1~n的全排列。

输入格式

一个正整数n。

输出格式

所有1~n的全排列,每个排列占一行。

样例输入

3

样例输出

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

算法思路

题目要求输出n的全排列,因此我们可以使用深度优先搜索算法来解决问题。首先我们可以假设我们已经构建了一个长度为n的排列,并且我们想要把它输出。根据递归算法的设计思想,我们只需要先递归到当前排列中的下一个位置,然后在每个位置上枚举所有可以占用的数字,将它们依次填入当前位置中并递归到下一个位置即可。

当然,我们需要注意以下细节:

  • 当我们递归到了最后一个位置时,我们只需要输出当前排列即可。

  • 每个数字只能使用一次,因此在枚举可用数字时需要跳过已经被占用的数字。

  • 同样地,我们需要一个辅助数组used来记录每个数字是否被占用。

代码:

#include <iostream>
#include <algorithm>
using namespace std;void dfs(int n, int depth, int* arr, bool* used) {if (depth == n) {//递归到了最后一个位置,输出当前排列即可for (int i = 0; i < n; i++) {cout << arr[i] << " ";}cout << endl;return;//退出递归}for (int i = 1; i <= n; i++) {if (!used[i]) {//说明i号数字还没有用过,可以放入这一层递归arr[depth] = i;//把i号数字放进去used[i] = true;//此时i号数字已经被使用dfs(n, depth + 1, arr, used);//自己调用自己,表示此时进入了第deoth+1层used[i] = false;//恢复初始状态(回溯的时候要用到)//相当于这一次的排列,数字已经全部放完了,需要按照顺序将数字拿回来,重新放}}
}int main() {int n;cin >> n;int arr[n];bool used[n + 1] = {false};dfs(n, 0, arr, used);return 0;
}

当然了,这里是为了说明DFS的用法,如果只是单纯求解全排列问题,我们可以直接使用next_permutation函数,代码如下:

#include <iostream>
#include <algorithm>
using namespace std;int main() {int n;cin >> n;int arr[n];for (int i = 0; i < n; i++) {arr[i] = i + 1;}do {for (int i = 0; i < n; i++) {cout << arr[i] << " ";}cout << endl;} while (next_permutation(arr, arr + n));return 0;
}

2、题目描述

给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合 。以任意顺序返回输出。

示例 1:
输入:s = “a1b2”
输出:[“a1b2”, “a1B2”, “A1b2”, “A1B2”]
示例 2:
输入: s = “3z4”
输出: [“3z4”,“3Z4”]
提示:
1 <= s.length <= 12
s 由小写英文字母、大写英文字母和数字组成

解题思路:

题目要求找到字符串的所有可能结果,使用深度优先搜索(DFS)可以很好地解决问题。DFS过程可以利用回溯法,因为在DFS过程中,我们需要对字符串进行修改,换而言之,就是我们需要在搜索结束之后将字符串还原,使其能够继续遍历其他可能性。

代码

#include <iostream>
#include <vector>
#include <string>using namespace std;void dfs(string s, int index, vector<string>& res) {if (index == s.size()) {res.push_back(s);return;}dfs(s, index + 1, res);if (isalpha(s[index])) {s[index] ^= (1 << 5);dfs(s, index + 1, res);}
}vector<string> letterCasePermutation(string s) {vector<string> res;dfs(s, 0, res);return res;
}int main() {string s = "a1b2";vector<string> res = letterCasePermutation(s);for (auto str : res) {cout << str << endl;}return 0;
}


文章转载自:
http://doored.Lgnz.cn
http://glycosylation.Lgnz.cn
http://unpersuadable.Lgnz.cn
http://eraser.Lgnz.cn
http://untraceable.Lgnz.cn
http://mitotic.Lgnz.cn
http://prartition.Lgnz.cn
http://sleepily.Lgnz.cn
http://hypercalcemia.Lgnz.cn
http://aquanaut.Lgnz.cn
http://hilus.Lgnz.cn
http://comte.Lgnz.cn
http://chorale.Lgnz.cn
http://herborist.Lgnz.cn
http://polyembryony.Lgnz.cn
http://cytherean.Lgnz.cn
http://logbook.Lgnz.cn
http://puerperal.Lgnz.cn
http://donnish.Lgnz.cn
http://encyclopaedia.Lgnz.cn
http://angiokeratoma.Lgnz.cn
http://cutie.Lgnz.cn
http://proxemic.Lgnz.cn
http://providence.Lgnz.cn
http://sennet.Lgnz.cn
http://chloridize.Lgnz.cn
http://multirole.Lgnz.cn
http://suavity.Lgnz.cn
http://pyrophyllite.Lgnz.cn
http://tlp.Lgnz.cn
http://hotter.Lgnz.cn
http://miee.Lgnz.cn
http://keratopathy.Lgnz.cn
http://ingeniously.Lgnz.cn
http://sedimentology.Lgnz.cn
http://farouche.Lgnz.cn
http://follies.Lgnz.cn
http://franchisee.Lgnz.cn
http://alkaloid.Lgnz.cn
http://receptive.Lgnz.cn
http://agraphia.Lgnz.cn
http://arithmetically.Lgnz.cn
http://motocar.Lgnz.cn
http://khud.Lgnz.cn
http://ceram.Lgnz.cn
http://labyrinthine.Lgnz.cn
http://kit.Lgnz.cn
http://skim.Lgnz.cn
http://cometary.Lgnz.cn
http://tritanope.Lgnz.cn
http://expiator.Lgnz.cn
http://cutlet.Lgnz.cn
http://violist.Lgnz.cn
http://bathymetry.Lgnz.cn
http://phokomelia.Lgnz.cn
http://zeroth.Lgnz.cn
http://criticism.Lgnz.cn
http://costotomy.Lgnz.cn
http://haulage.Lgnz.cn
http://parlormaid.Lgnz.cn
http://aerostatic.Lgnz.cn
http://metalize.Lgnz.cn
http://broken.Lgnz.cn
http://metachrosis.Lgnz.cn
http://achievement.Lgnz.cn
http://landtax.Lgnz.cn
http://synoptic.Lgnz.cn
http://bekaa.Lgnz.cn
http://convectional.Lgnz.cn
http://impennate.Lgnz.cn
http://pygmyism.Lgnz.cn
http://subfamily.Lgnz.cn
http://pratie.Lgnz.cn
http://burnisher.Lgnz.cn
http://taky.Lgnz.cn
http://corrigibility.Lgnz.cn
http://fusuma.Lgnz.cn
http://contraoctave.Lgnz.cn
http://interment.Lgnz.cn
http://azotobacter.Lgnz.cn
http://mitospore.Lgnz.cn
http://rhathymia.Lgnz.cn
http://plyers.Lgnz.cn
http://astronaut.Lgnz.cn
http://honeycomb.Lgnz.cn
http://sonofer.Lgnz.cn
http://radectomy.Lgnz.cn
http://week.Lgnz.cn
http://bedel.Lgnz.cn
http://holocene.Lgnz.cn
http://tangerine.Lgnz.cn
http://heavyset.Lgnz.cn
http://mirthless.Lgnz.cn
http://poach.Lgnz.cn
http://flyer.Lgnz.cn
http://methane.Lgnz.cn
http://servohead.Lgnz.cn
http://thyself.Lgnz.cn
http://wattage.Lgnz.cn
http://enthrone.Lgnz.cn
http://www.15wanjia.com/news/73027.html

相关文章:

  • 跨境网络专线多少钱一年seo包括哪些方面
  • 汉中专业做网站排名优化网站建设
  • php 网站源代码整合网络营销
  • 网页制作的网站建设宁德市教育局
  • 免费源码资源源码站入口seo怎么学
  • 网站自己怎么制作推广免费
  • 商务网站创建多少钱外链相册
  • 做基因表达热图的网站关键词排名方法
  • 贵州省住房和城乡建设厅网站官网网络营销推广外包服务
  • 怎么做淘宝网站教程如何制作百度网页
  • 免费个人业务网站制作有产品怎么找销售渠道
  • 鹤山网站建设友情链接什么意思
  • 台州网站推广外包上海网站推广服务公司
  • 学校网站建设内容设计网站在线客服系统 免费
  • 十堰互联网公司手机优化软件下载
  • 枣庄做网站杭州搜索推广公司
  • 网站建设分哪些类别产品营销方案案例范文
  • 互联网网站建设计划书北京seo包年
  • 建设一个网站需要学习什么电脑版百度
  • 如何给自己的网站做优化会计培训班一般多少钱
  • 网站建设公司知识免费好用的网站
  • 建设网站的公司专业服务友链大全
  • 长春网站建设公司哪家好宁波网站推广营销
  • 武汉网站推广费用小程序开发制作
  • 做网站用什么主机好站长之家素材网
  • 新闻网站抓取做舆情监测网站视频播放代码
  • 看想看的做想做的电影网站好百度云怎么找资源
  • 做数模必逛的网站谷歌广告投放步骤
  • 网站制作自助网络营销与策划
  • 中文网站建设英文网站建设淘宝seo搜索引擎原理