反序输出
反序输出
http://www.nowcoder.com/questionTerminal/171278d170c64d998ab342b3b40171bb
很简单的翻转字符串,原地转换即可。可以直接用库函数,但是不推荐。
#include<iostream> #include<algorithm> using namespace std; string reverseStr(string s){ int n = s.size(); for(int i = 0; i < n/2; i ++) swap(s[i], s[n - i - 1]); return s; } int main(){ string s; while(cin >> s){ reverse(s.begin(), s.end()); cout << s << endl; // cout << reverseStr(s) << endl; } return 0; }
算法题解 文章被收录于专栏
不定期更新一些算法题解,有什么问题可以随时留言~