Bailian2752 字元串數組排序問題
問題簡述:(略)
問題分析:簡單的字元串排序問題,不解釋。
程式說明:(略)
參考連結:(略)
題記:(略)
AC的C++語言程式如下:
/* Bailian2752 字元串數組排序問題 */
#include <bits/stdc++.h>
using namespace std;
const int N = 50;
string s[N];
bool cmp(string a, string b)
{
int len = min(a.size(), b.size());
for(int i = 0; i <= len; i++)
if(toupper(a[i]) < toupper(b[i])) return true;
else if(toupper(a[i]) > toupper(b[i])) return false;
return true;
}
int main()
{
string p;
int n;
cin >> p >> n;
getline(cin, s[0]);
for(int i = 0; i < n; i++)
getline(cin, s[i]);
if(p == "inc") {
sort(s, s + n);
for(int i = 0; i < n; i++)
cout << s[i] << endl;
} else if(p == "dec") {
sort(s, s + n);
for(int i = n - 1; i >= 0; i--)
cout << s[i] << endl;
} else if(p == "ncinc") {
sort(s, s + n, cmp);
for(int i = 0; i < n; i++)
cout << s[i] << endl;
} else if(p == "ncdec") {
sort(s, s + n, cmp);
for(int i = n - 1; i >= 0; i--)
cout << s[i] << endl;
}
return 0;
}