T1:倒置字符串
链接:倒置字符串__牛客网
将一句话的单词进行倒置,标点不倒置。比如 I like beijing. 经过函数后变为:beijing. like I


- #include
- #include
- using namespace std;
- vector
ans,res; - string s1;
- int main()
- {
- while(cin>>s1){
- ans.push_back(s1);
- }
- int n=ans.size();
- for(int i=n-1;i>=0;i--){
- res.push_back(ans[i]);
- }
- for(auto x:res){
- cout<
" "; - }
- return 0;
- }
可以借助cin读入字符串的特点:读到空格就会停止。
将每个读入的字符串存入数组其中一个数组中,然后逆序到另一个数组中。
T2:排序子序列
牛牛定义排序子序列为一个数组中一段连续的子序列,并且这段子序列是非递增或者非递减排序的。牛牛有一个长度为n的整数数组A,他现在有一个任务是把数组A分为若干段排序子序列,牛牛想知道他最少可以把这个数组分为几段排序子序列.
如样例所示,牛牛可以把数组A划分为[1,2,3]和[2,2,1]两个排序子序列,至少需要划分为2个排序子序列,所以输出2


非递增:递减或者相等 a[i-1]>=a[i-1]
非递减:递增或者相等 a[i-1]<=a[i-1]
- #include
- #include
- using namespace std;
- int n;
- int main()
- {
- cin>>n;
- vector<int>a;
- a.resize(n+1);
- for(int i=0;i
- cin>>a[i];
- }
- a[n]=0;
- int idx=0;
- int ans=0;
- while(idx
- if(a[idx]1]){
- while(idx
1]){ - idx++;
- }
- ans++;
- idx++;
- }
- else if(a[idx]==a[idx+1]) idx++;
- else {
- while(idx
=a[idx+1]){ - idx++;
- }
- ans++;
- idx++;
- }
- }
- cout<
- return 0;
- }
因为每个数字都大于1,所以多开了一个位置a[n]=0,防止边界情况出现
-
相关阅读:
速看|期待已久的2022年广州助理检测工程师真题解析终于出炉
Spring Boot与Kubernetes:现代云部署的完美组合
c2rust简单使用
【python】网络爬虫与信息提取--scrapy爬虫框架介绍
HyperLynx(十七)SATA的设计与仿真
工业智能网关BL110应用之五十一: 数据上传云金鸽MQTT的配置
@Mapper与@MapperScan注解
169. 多数元素
TypeError: ‘005393.jpg‘ has type str, but expected one of: bytes
在安全数字包裹机制下,汽车制造业如何安全可控地实现上下游数据协作?
-
原文地址:https://blog.csdn.net/m0_64263546/article/details/133242207