This is a sort + string handling the subject, the difficulty belongs to the PAT B, five questions in the third question.
Analysis: lexicographic order is the order of the string in the dictionary. E.g,
1. "A" is lexicographically less than "a";
2. "A" is lexicographically less than "b";
3. "Aa" lexicographic order is less than "aaaa"
4. "Aaaa" is lexicographically smaller than "aab" dictionary order, from the most significant bit here, a comparison of a character's character.
5. "Aa" lexicographic order is less than "aa"
#include<iostream> #include<algorithm> using namespace std; bool cmp(string a,string b) { //计算单词ASCII值,并转小写单词 int sum1 = 0,sum2 = 0; for(int i = 0 ; i < a.size(); ++i) { sum1 += a[i]; a[i] = tolower(a[i]); } for(int i = 0 ; i < b.size(); ++i) { sum2 +=B [I]; B [I] = tolower (B [I]); } IF (! A = B) return A <B; // Comparative lowercase words the else return SUM1 <SUM2; // the same word, it is more ASCII and } int main () { int n; String STR [ 100 ]; CIN >> n; for ( int I = 0 ; I <n; ++ I) // n words input CIN >> STR [I]; Sort (STR, STR + n-, CMP); // the converted into lowercase words lexicographic, the original word ASCII code,对单词排序 for(int i = 0; i < n;++i){ if(i > 0) printf(" "); cout<<str[i]; } return 0; }