输入包含多组数据。
每组数据第一行包含一个正整数n (1≤n≤1000)。
紧接着n行,每行包含一个书名和这本书的所属类别。书名和类别名都只有字母构成,并且长度均不超过32个字符。
对应每一组数据,按照要求先按照类别排序(大小写无关),再按照书名排序(大小写无关),最后输出排好序的书名,每一个书名占一行。
每一组数据之后输出一个空行做分隔符。
3 JavaScriptInAction Programming OnLisp Lisp LetOverLambda Lisp 3 abc ZZZ XXX AAA DEF AAA
LetOverLambda OnLisp JavaScriptInAction DEF XXX abc
from collections import defaultdict
while True:
try:
dd = defaultdict(list)
for _ in range(int(input())):
bookName, typeName = input().split()
dd[typeName].append(bookName)
for i in sorted(dd.keys(),key=lambda c:c.upper()):
for j in sorted(dd[i],key=lambda c:c.upper()):
print (j)
print ()
except:
break
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<functional> #include <map> #include <set> #include <unordered_set> #include <unordered_map> #include <exception> #include <iomanip> #include <memory> #include <sstream> #define INF 1000000 using namespace std; typedef pair<string, string> StrPair; int main(int argc, char** argv) { //freopen("in.txt", "r", stdin); int n; while (cin >> n) { string name, type; vector<StrPair> shelf; unordered_map<string, string> hs; shelf.reserve(n); for (int i = 0; i < n; ++i) { cin >> name >> type; string lower_name = name; for (auto& c : lower_name) c = tolower(c); for (auto& c : type) c = tolower(c); hs.emplace(lower_name, name); shelf.emplace_back(lower_name, type); } sort(shelf.begin(), shelf.end(), [](const StrPair& o1, const StrPair& o2) { return o1.second < o2.second || o1.second == o2.second && o1.first < o2.first; }); for (auto& pr : shelf) cout << hs[pr.first] << endl; cout << endl; } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); Book[] books = new Book[n]; for (int i = 0; i < books.length; i ++ ) books[i] = new Book(sc.next(), sc.next()); Arrays.sort(books); for (Book book:books) System.out.println(book.name); System.out.println(); } } static class Book implements Comparable<Book> { String name; String category; public Book(String name, String category) { this.name = name; this.category = category; } @Override public int compareTo(Book o) { int k = this.category.compareToIgnoreCase(o.category); if(k == 0) return this.name.compareToIgnoreCase(o.name); return k; } } }
链接:https://www.nowcoder.com/questionTerminal/641ddd0fb98b406690a086e3f36b20b1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <iostream> #include <string> #include <vector> #include <map> #include <set> usingnamespacestd; classbookSort :publicless<string> // 定义升序比较器 { public: // 定义大小写无关的比较器 booloperator()(string a, string b)const { for(char& ch : a) ch =tolower(ch); for(char& ch : b) ch =tolower(ch); returna < b; // 返回字典序 } }; vector<string> solve(map<string, set<string, bookSort>, bookSort> & store) { vector<string> list; // 对于mao中的每一个key(目录名)值,输出对应的所有value(书名)。 for(auto pairs : store) { string category = pairs.first; // 目录名 for(auto name : store[category]) // 每个书名 list.push_back(name); // 将书名存入到最终输出列表内 } returnlist; // 输出列表返回给main函数 } intmain() { intn, flag = 0; while(cin >> n && n) { map<string, set<string, bookSort>, bookSort> store; string name, category; for(inti = 0; i < n; ++i) { cin >> name >> category; // 读入目录名和书名 store[category].insert(name); // 根据目录名,将书名插入到 set 集合 } vector<string> list = solve(store); // 获取处理后的书名列表 for(auto str : list) // 输出所有书名 cout << str << endl; cout << endl; } return0; } |
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> using namespace std; typedef pair<string, string> PAIR; bool cmp(const PAIR &x, const PAIR &y) { return x.first < y.first; } typedef struct { string classname; vector<PAIR> classdata; void nodesort() { sort(classdata.begin(), classdata.end(), cmp); } }node; int main() { int length; string bookname, bookclass; while (cin >> length) { map<string, node> data; for (int i = 0; i < length; i++) { cin >> bookname >> bookclass; string firstname; for (auto c : bookname) firstname += tolower(c); for (auto &c : bookclass) c = tolower(c); PAIR p = make_pair(firstname, bookname); data[bookclass].classdata.push_back(p); } for (map<string, node>::iterator it = data.begin(); it != data.end(); it++) { it->second.nodesort(); for (vector<PAIR>::iterator k = it->second.classdata.begin(); k != it->second.classdata.end(); k++) cout << k->second << endl; } cout << endl; } return 0; }
#include <iostream> #include <string> #include <vector> #include <map> #include <set> using namespace std; class bookSort : public less<string> // 定义升序比较器 { public: // 定义大小写无关的比较器 bool operator()(string a, string b) const { for (char & ch : a) ch = tolower(ch); for (char & ch : b) ch = tolower(ch); return a < b; // 返回字典序 } }; vector<string> solve(map<string, set<string, bookSort>, bookSort> & store) { vector<string> list; // 对于mao中的每一个key(目录名)值,输出对应的所有value(书名)。 for (auto pairs : store) { string category = pairs.first; // 目录名 for (auto name : store[category]) // 每个书名 list.push_back(name); // 将书名存入到最终输出列表内 } return list; // 输出列表返回给main函数 } int main() { int n, flag = 0; while (cin >> n && n) { map<string, set<string, bookSort>, bookSort> store; string name, category; for (int i = 0; i < n; ++i) { cin >> name >> category; // 读入目录名和书名 store[category].insert(name); // 根据目录名,将书名插入到 set 集合 } vector<string> list = solve(store); // 获取处理后的书名列表 for (auto str : list) // 输出所有书名 cout << str << endl; cout << endl; } return 0; }