首页 > 代码库 > UVA - 10815

UVA - 10815

 

主要学习一下怎么利用sort对二维字符数组排序

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int maxn = 3e5 + 10;
 7 char s[maxn][50];
 8 char *str[maxn];
 9 
10 //
11 bool cmp(const char *s1, const char *s2) {
12     return strcmp(s1, s2) < 0;
13 }
14 
15 int main(int argc, const char * argv[]) {
16     int cnt = 0, p = 0;
17     char c;
18     while ((c = getchar()) != EOF) {
19         //if (c == ‘#‘) break;
20         if (A <= c && c <= Z) {
21             c = c - A + a;
22             s[cnt][p++] = c;
23         } else if (a <= c && c <= z) {
24             s[cnt][p++] = c;
25         } else {
26             if (p != 0) {
27                 s[cnt][p] = \0;
28                 str[cnt] = s[cnt];
29                 cnt++;
30                 p = 0;
31             }
32         }
33     }
34     sort(str, str + cnt, cmp);//
35     puts(str[0]);
36     for (int i = 1; i < cnt; i++) {
37         if (strcmp(str[i], str[i - 1]) != 0) {
38             puts(str[i]);
39         }
40     }
41     return 0;
42 }
View Code

 

或者就写成结构体,就转化为对结构体进行排序

UVA - 10815