字串的连接最长路径查找

题目描述

  给定n个字符串,请对n个字符串按照字典序排列。 

输入描述

  输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。

输出描述

  数据输出n行,输出结果为按照字典序排列的字符串。

输入样例

9
cap
to
cat
card
two
too
up
boat
boot

输出样例

boat
boot
cap
card
cat
to
too
two
up

测试代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <assert.h>
 5 
 6 int cmp(const void *p, const void *q)
 7 {
 8     return strcmp(*(char **)p, *(char **)q);
 9 }
10 
11 int main(void)
12 {
13     char **p = NULL;
14     int n, i;
15     scanf("%d", &n);
16     p = (char **)malloc(n * sizeof(char *));
17     assert(p != NULL);
18     for (i = 0; i < n; i++)
19     {
20         p[i] = (char *)malloc(105 * sizeof(char));
21         assert(p[i] != NULL);
22         scanf("%s", p[i]);
23     }
24     qsort(p, n, sizeof(p[0]), cmp);
25     for (i = 0; i < n; i++)
26     {
27         puts(*(p + i));
28     }
29     return 0;
30 }

 

posted @ 2016-06-19 22:12  新生代黑马  阅读(339)  评论(0编辑  收藏  举报