题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
#include <iostream> #include <vector> #include <string> using namespace std; //s.compare(s1) ; 等于的话,为0,小于的话为负数,大于的话,为非负数 int main() { int n; while (cin >> n) { vector<string>v(n); string temp; for (int i = 0; i < n; i++) { cin >> temp; v[i] = temp; } int ii = 0; //要交换的标志位 int curPos = 0; string minString; for (int i = 0; i < v.size(); i++) { minString = v[i]; for (int j = i + 1; j < v.size(); j++) { if (minString.compare(v[j]) > 0) { minString = v[j]; curPos = j; //记下当前的位置 ii = 1; } } //有更小的话,那就将最小的放在前面 if (ii == 1) { ii = 0; temp = v[i]; v[i] = minString; v[curPos] = temp; } } for (int i = 0; i < v.size(); i++) { cout << v[i] << endl; } } return 0; } // 64 位输出请用 printf("%lld")