问题 1755: 姓名排序 (优先队列)

题目链接:https://www.dotcpp.com/oj/problem1755.html

题目描述

存储一组姓名,如Apple,Tom,Green,Jack 要求按照字典序排序并显示。

输入

输入第一行为样例数m,对于每个样例,第一行为人数n,接下来有n个姓名,n不超过10,每个名字长度不超过20。

输出

对于每个样例输出排序后的结果,每行一个姓名。

样例输入
1
4
Apple
Tom
Green
Jack 
样例输出
Apple
Green
Jack
Tom

优先队列很方便

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath> 
 4 #include <string>
 5 #include <cstring>
 6 #include <map> 
 7 #include <cstdio>
 8 #include <queue>
 9 using namespace std;
10 int n,t;
11 string s;
12 int main()
13 {
14     cin>>t;
15     while(t--){
16         cin>>n;
17         priority_queue<string, vector<string> ,greater<string> > pq;
18         while(n--){
19             cin>>s;
20             pq.push(s);
21         }
22         while(pq.size()){
23             cout<<pq.top()<<endl;
24             pq.pop();
25         }
26     }
27     return 0;
28 }

猜你喜欢

转载自www.cnblogs.com/shixinzei/p/10746113.html