当前位置:网站首页>【编程强训2】排序子序列+倒置字符串
【编程强训2】排序子序列+倒置字符串
2022-07-01 07:07:00 【…狂奔的蜗牛~】
1.排序子序列 -> 链接

【题目解析】:
本题要求解的是排序子序列,排序子序列为非递增或者非递减,很多同学在这个非递增、非递减问题上很纠结,
注意:非递减就是a[i]<=a[i+1],递减就是a[i]>a[i+1],非递增就是a[i]>=a[i+1],递增就是a[i]<a[i+1]。
【解题思路】:
- 本题依次比较整个数组
- a[i+1]>a[i] ,则进入非递减序列判断,直到遍历到下一个值不大于等于为止count++,然后进行下一位置
的判断 - a[i+1]<a[i],则进入非递增序列判断,直到遍历到下一个值不小于等于为止count++,然后进行下一位置
的判断 - a[i+1] == a[i]不进行操作,++i进行下一位置遍历,因为相等既可以属于非递增序列,也可以属于非递减序
列。
【本题注意点】:
本题开始a[i+1]与a[i]进行比较,为了避免越界,数组定义为n+1个,同时给a[n] = 0;
a[n] = 0带来的影响,我们分为三种情况讨论:
- 若到a[n-1] 的最后一组是非递减序列,当i= =n-1,a[i] >a[i+1],因为前面的数都是大于0的,这个输入条件已经说明了(去看看题目输入条件描述),里面的循环结束,i++,count++,i==n,外面的循环结束。
- 若到a[n-1] 的最后一组是非递增序列,当i= =n-1,a[i] >a[i+1],因为前面的数都是大于0的,这个输入条 件已经说明了(去看看题目输入条件描述),循环再走一次,i++, i== n,里面的循环结束,i++,
count++,i==n+1,外面的循环结束。- 第三种情况 1 2 1 2 1最后一个数是单独的情况,后面补个0,序列变成1 2 1 2 1 0,当走完全面的序列 i==n-1时,a[i] > a[i+1],进入判断出一个非递增序列,count++,i++,循环结束。
也就是说数组最后一个位置多增加一个0,不会影响第1、2情况的判断,主要是帮助第3情况的正确判断。
代码实现:
#include<iostream>
#include<vector>
using namespace std;
// 本题牛客测试用例不全,至少应该增加以下两组测试用例
// 输入:
// 4
// 1 3 2 3
// 输出:2
// 输入:
// 6
// 3 2 1 1 2 3
// 输出:2
int main()
{
int n;
cin >> n;
// 注意这里多给了一个值,是处理越界的情况的比较,具体参考上面的解题思路
vector<int> a;
a.resize(n + 1);//这里有个坑,这个题越界了牛客测不出来,给n,并且不写a[n] = 0;不会报错,但是最好写上
a[n] = 0;
//读入数组
int i = 0;
for (i = 0; i < n; ++i)
cin >> a[i];
i = 0;
int count = 0;
while (i < n)
{
// 非递减子序列
if (a[i] < a[i + 1])
{
while (i < n && a[i] <= a[i + 1])
i++;
count++;
i++;
}
else if (a[i] == a[i + 1])
{
i++;
} else // 非递增子序列
{
while (i < n && a[i] >= a[i + 1])
i++;
count++;
i++;
}
}
cout << count << endl;
return 0;
2.倒置字符串 -> 链接

【题目解析】:
本题题意很简单,就是将一段字符串中的前后单词交换,以单词为单位逆置。
【解题思路1】:
先将整个字符串逆置过来,再遍历字符串,找出每个单词,对单词逆置。这里我们使用了stl算法中的reverse,所以这里使用迭代器遍历string
代码实现:
include <iostream>
include <string>
include <algorithm>
using namespace std;
int main()
{
string s;
// 注意这里要使用getline,cin>>s遇到空格就接收结束了
getline(cin, s);
// 翻转整个句子
reverse(s.begin(), s.end());
// 翻转单词
auto start = s.begin();
while (start != s.end())
{
auto end = start;
while (end != s.end() && *end != ' ')
end++;
reverse(start, end);
if (end != s.end())
start = end + 1;
else{
start = end;
}
cout << s << endl;
return 0;
}
边栏推荐
- C# 读写自定义的Config文件
- 8 张图 | 剖析 Eureka 的首次同步注册表
- 记一次线上接口慢查询问题排查
- DC-4靶机
- 热烈祝贺五行和合酒成功挂牌
- Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
- Pourquoi tant de gens sont - ils devenus des gestionnaires de produits? Quelles sont les perspectives de développement des gestionnaires de produits?
- MySQL table partition creation method
- Introduction to spark (one article is enough)
- ctfshow-web352,353(SSRF)
猜你喜欢

解决无法读取META-INF.services里面定义的类
![[matlab] solve nonlinear programming](/img/2e/7a1f520b602b7539be479efb198f6a.png)
[matlab] solve nonlinear programming

Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space

【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真

Solve the problem that the class defined in meta-inf.services cannot be read

【深圳IO】精确食品称(汇编语言的一些理解)

Understand esp32 sleep mode and its power consumption

为什么这么多人转行产品经理?产品经理发展前景如何?
![C language implementation [minesweeping game] full version (implementation source code)](/img/70/60f9a61bd99fa5fb5fab679a32528e.png)
C language implementation [minesweeping game] full version (implementation source code)

【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
随机推荐
Pourquoi tant de gens sont - ils devenus des gestionnaires de produits? Quelles sont les perspectives de développement des gestionnaires de produits?
Esp32 esp-idf ADC monitors battery voltage (with correction)
未来互联网人才还稀缺吗?哪些技术方向热门?
Rclone Chinese document: a collection of common commands
Is fixed investment fund a high-risk product?
Code practice - build your own diffusion models / score based generic models from scratch
Esp32 esp-idf GPIO key interrupt response
Mysql与Redis一致性解决方案
用手机在指南针上开户靠谱吗?这样有没有什么安全隐患
JAX的深度学习和科学计算
Record an online interface slow query problem troubleshooting
Which securities company does qiniu school cooperate with? Is it safe to open an account?
广发证券开户是安全可靠的么?怎么开广发证券账户
STM32F1与STM32CubeIDE编程实例-NEC协议红外接收与解码
代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
【图像处理】图像直方图均衡化系统含GUI界面
热烈祝贺五行和合酒成功挂牌
[the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
C语言实现【三子棋游戏】(步骤分析和实现源码)
为什么这么多人转行产品经理?产品经理发展前景如何?