码迷,mamicode.com
首页 > 编程语言 > 详细

字符串排序

时间:2016-05-23 10:39:23      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

题目描述

  给定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

C/C++测试代码1

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int n;
 8     string str[1000], temp;
 9     while (cin >> n)
10     {
11         for (int i = 0; i < n; i++)
12         {
13             cin >> str[i];
14         }
15         //C++的string类对>、<这类比较运算符进行了重载,可以直接用来比较字符串
16         for (int i = 0; i < n - 1; i++)
17         {
18             for (int j = 0; j < n - 1 - i; j++)
19             {
20                 if (str[j] > str[j + 1])
21                 {
22                     temp = str[j];
23                     str[j] = str[j + 1];
24                     str[j + 1] = temp;
25                 }
26             }
27         }
28         for (int i = 0; i < n; i++)
29         {
30             cout << str[i] << endl;
31         }
32     }
33     return 0;
34 }

C/C++测试代码2

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n;
 9     string str, temp;
10     map<string, int> res;
11     while (cin >> n)
12     {
13         for (int i = 0; i < n; i++)
14         {
15             cin >> str;
16             res[str]++;
17         }
18         for (map<string, int>::iterator it = res.begin(); it != res.end(); it++)
19         {
20             while (it->second--)
21             {
22                 cout << it->first << endl;
23             }
24         }
25     }
26     return 0;
27 }

C/C++测试代码3

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.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()
12 {
13     int n;
14     char **p = NULL;
15     scanf("%d", &n);
16     p = (char **)malloc(n * sizeof(char *));
17     assert(p != NULL);
18     //成功开辟空间并接收字符串
19     for (int i = 0; i < n; i++)
20     {
21         p[i] = (char *)malloc(105 * sizeof(char));
22         assert(p[i] != NULL);
23         scanf("%s", p[i]);
24     }
25     qsort(p, n, sizeof(p[0]), cmp);
26     for (int i = 0; i < n; i++)
27     {
28         puts(p[i]);
29     }
30     free(p);
31     return 0;
32 }

Java测试代码1

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         Scanner sc = new Scanner(System.in);
 7         int n = sc.nextInt();
 8         String[] str = new String[n];
 9         for (int i = 0; i < n; i++) {
10             str[i] = sc.nextLine();
11         }
12         Arrays.sort(str);
13         for(int i = 0; i < str.length; i++) {
14             System.out.println(str[i]);
15         }
16         sc.close();
17     }
18 }

Java测试代码2

 1 import java.util.ArrayList;
 2 import java.util.Collections;
 3 import java.util.Iterator;
 4 import java.util.List;
 5 import java.util.Scanner;
 6 
 7 public class Main {
 8     public static void main(String[] args) {
 9         Scanner sc = new Scanner(System.in);
10         int n = sc.nextInt();
11         List<String> list = new ArrayList<String>();
12         for (int i = 0; i < n; i++) {
13             list.add(sc.nextLine());
14         }
15         sc.close();
16         Collections.sort(list);
17         Iterator<String> it = list.iterator();
18         while (it.hasNext()) {
19             System.out.println(it.next());
20         }
21     }
22 }

字符串排序

标签:

原文地址:http://www.cnblogs.com/maxin/p/5518758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!