竞赛
考级
#include<iostream> using namespace std; int main(){ string s; int l,r; cin>>l>>r; cin>>s; l++; r--; while(l<r){ char t=s[l]; s[l]=s[r]; s[r]=t; l--; r--; } cout<<s; return 0; }
题目大意 给定一个字符串和一个区间,输出翻转这段区间后的字符串。 解题思路 直接模拟或使用 reverse 函数即可。 参考代码 方法一 方法二
这道题使用reverse函数反转特定区域的数据即可
#include<bits/stdc++.h> using namespace std; int main(){ string c; int l,r; cin>>l>>r; cin>>c; l--; r--; while(l<r){ char t=c[l]; c[l]=c[r]; c[r]=t; l++; r--; } cout<<c; }
提交答案之后,这里将显示提交结果~