日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

PTA-1022——Digital Library

發(fā)布時(shí)間:2025/3/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PTA-1022——Digital Library 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer?N?(≤) which is the total number of books. Then?N?blocks follow, each contains the information of a book in 6 lines:

  • Line #1: the 7-digit ID number;
  • Line #2: the book title -- a string of no more than 80 characters;
  • Line #3: the author -- a string of no more than 80 characters;
  • Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
  • Line #5: the publisher -- a string of no more than 80 characters;
  • Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].

It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer?M?(≤) which is the number of user's search queries. Then?M?lines follow, each in one of the formats shown below:

  • 1: a book title
  • 2: name of an author
  • 3: a key word
  • 4: name of a publisher
  • 5: a 4-digit number representing the year

Output Specification:

For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print?Not Found?instead.

Sample Input:

3 1111111 The Testing Book Yue Chen test code debug sort keywords ZUCS Print 2011 3333333 Another Testing Book Yue Chen test code sort keywords ZUCS Print2 2012 2222222 The Testing Book CYLL keywords debug book ZUCS Print2 2011 6 1: The Testing Book 2: Yue Chen 3: keywords 4: ZUCS Print 5: 2011 3: blablabla

Sample Output:

1: The Testing Book 1111111 2222222 2: Yue Chen 1111111 3333333 3: keywords 1111111 2222222 3333333 4: ZUCS Print 1111111 5: 2011 1111111 2222222 3: blablabla Not Found

分析:

模擬題。注意帶空格的字符串輸入,點(diǎn)擊前往

此題更好的方法是用map映射,這邊沒有用。

代碼:

1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 #include<cstring> 5 using namespace std; 6 int n; 7 int m; 8 struct Book{ 9 int id; 10 string name; 11 string author; 12 vector<string> keywords; 13 string publisher; 14 string year; 15 }; 16 17 Book books[10001]; //書的集合 18 vector<int> ans; 19 20 bool cmp(Book a,Book b){ //根據(jù)ID快排 21 return a.id<b.id; 22 } 23 24 void findByTitle(string query){ //第一種情況,根據(jù)書名查詢 25 for(int i=0;i<n;i++){ 26 if(books[i].name==query){ 27 ans.push_back(books[i].id); 28 } 29 } 30 } 31 32 void findByAuthor(string query){ //第二種情況,根據(jù)作者查詢 33 for(int i=0;i<n;i++){ 34 if(books[i].author==query){ 35 ans.push_back(books[i].id); 36 } 37 } 38 } 39 40 void findByWord(string query){ //第三種情況,根據(jù)關(guān)鍵字查詢 41 for(int i=0;i<n;i++){ 42 for(vector<string>::iterator it=books[i].keywords.begin();it!=books[i].keywords.end();it++){ 43 if(*it==query){ 44 ans.push_back(books[i].id); 45 break; 46 } 47 } 48 } 49 } 50 51 void findByPublisher(string query){ //第四種情況,根據(jù)出版社查詢 52 for(int i=0;i<n;i++){ 53 if(books[i].publisher==query){ 54 ans.push_back(books[i].id); 55 } 56 } 57 } 58 59 void findByYear(string query){ //第五種情況,根據(jù)年份查詢 60 for(int i=0;i<n;i++){ 61 if(books[i].year==query){ 62 ans.push_back(books[i].id); 63 } 64 } 65 } 66 67 int main(){ 68 cin>>n; 69 for(int i=0;i<n;i++){ 70 cin>>books[i].id; 71 cin.ignore(); //在cin和getline之間要加ignore函數(shù) 72 getline(cin,books[i].name); //輸入含有空格的字符串可以使用getline 73 getline(cin,books[i].author); 74 string tempKeywords; 75 getline(cin,tempKeywords); 76 int k=0; 77 while(!tempKeywords.empty()){ //關(guān)鍵字按空格進(jìn)行劃分 78 if(tempKeywords[k]==' '){ 79 books[i].keywords.push_back(tempKeywords.substr(0,k)); 80 tempKeywords=tempKeywords.substr(k+1); 81 k=0; 82 }else if(k==tempKeywords.length()){ 83 books[i].keywords.push_back(tempKeywords.substr(0,k)); 84 tempKeywords=""; 85 k=0; 86 }else{ 87 k++; 88 } 89 } 90 getline(cin,books[i].publisher); 91 getline(cin,books[i].year); 92 } 93 sort(books,books+n,cmp); //對(duì)書進(jìn)行排序 94 cin>>m; 95 for(int i=0;i<m;i++){ 96 int choice; 97 string query; 98 scanf("%d: ",&choice); 99 getline(cin,query); 100 ans.clear(); 101 switch(choice){ 102 case 1: findByTitle(query);break; 103 case 2: findByAuthor(query);break; 104 case 3: findByWord(query);break; 105 case 4: findByPublisher(query);break; 106 case 5: findByYear(query);break; 107 } 108 cout<<choice<<": "<<query<<endl; 109 if(ans.size()==0){ 110 cout<<"Not Found"<<endl; 111 }else{ 112 for(vector<int>::iterator it=ans.begin();it!=ans.end();it++){ 113 printf("%07d\n", *it); 114 } 115 } 116 } 117 return 0; 118 }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/orangecyh/p/10348908.html

總結(jié)

以上是生活随笔為你收集整理的PTA-1022——Digital Library的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。