PTA甲级考试真题练习22——1022 Digital Library
程序员文章站
2022-06-07 13:14:26
...
题目
思路
水题
代码
#include <iostream>
#include <string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<sstream>
using namespace std;
#define nmax 100
#define inf 999999
int booknum,quertnum;
typedef struct book
{
int ID;
string title;
string author;
vector<string> key;
string publisher;
string publish_year;
bool operator < (const book& a) const
{
return ID < a.ID;
}
}book;
set<book> library;
int Type;
string querywords;
vector<string> ans;
int main()
{
cin >> booknum;
for (int i = 0; i < booknum; ++i)
{
book tmp;
string keyArray;
cin >> tmp.ID;
//防止下一行输入捕获到回车
cin.ignore();
getline(cin, tmp.title);
getline(cin, tmp.author);
string key;
while (cin >> key)
{
tmp.key.push_back(key);
char c = getchar();
if (c == '\n')
break;
}
getline(cin, tmp.publisher);
getline(cin, tmp.publish_year);
library.insert(tmp);
}
cin >> quertnum;
for (int i = 0; i < quertnum; ++i)
{
stringstream s;
scanf("%d: ", &Type);
getline(cin, querywords);
char tmp_c[100] = "";
if (Type == 5)
{
sprintf(tmp_c, "%d: %04d",Type,atoi(querywords.c_str()));
ans.push_back(tmp_c);
}
else
{
s << Type << ": " << querywords;
ans.push_back(s.str());
}
bool isFound = false;
switch (Type)
{
case 1:
for (auto& p : library)
{
if (p.title == querywords)
{
isFound = true;
sprintf(tmp_c,"%07d",p.ID);
ans.push_back(tmp_c);
}
}
if (!isFound)
ans.push_back("Not Found");
break;
case 2:
for (auto& p : library)
{
if (p.author == querywords)
{
isFound = true;
sprintf(tmp_c, "%07d", p.ID);
ans.push_back(tmp_c);
}
}
if (!isFound)
ans.push_back("Not Found");
break;
case 3:
for (auto& p : library)
{
for (auto& keywords : p.key)
{
if (keywords == querywords)
{
isFound = true;
sprintf(tmp_c, "%07d", p.ID);
ans.push_back(tmp_c);
break;
}
}
}
if (!isFound)
ans.push_back("Not Found");
break;
case 4:
for (auto& p : library)
{
if (p.publisher == querywords)
{
isFound = true;
sprintf(tmp_c, "%07d", p.ID);
ans.push_back(tmp_c);
}
}
if (!isFound)
ans.push_back("Not Found");
break;
case 5:
for (auto& p : library)
{
if (p.publish_year == querywords)
{
isFound = true;
sprintf(tmp_c, "%07d", p.ID);
ans.push_back(tmp_c);
}
}
if (!isFound)
ans.push_back("Not Found");
break;
default:
break;
}
}
int i;
for(i = 0;i<ans.size()-1;++i)
{
cout<<ans[i]<<endl;
}
cout<<ans[i];
return 0;
}
上一篇: PAT-1019 数字黑洞 (20分)
下一篇: 牛客SQL练习-74-考试分数(三)
推荐阅读
-
PTA甲级考试真题练习111——1111 Online Map
-
PTA甲级考试真题练习22——1022 Digital Library
-
PTA甲级考试真题练习18——1018 Public Bike Management
-
PTA甲级考试真题练习20——1020 Tree Traversals
-
PTA甲级考试真题练习30——1030 Travel Plan
-
PTA甲级考试真题练习17——1017 Queueing at Bank
-
PTA甲级考试真题练习8——1008 Elevator
-
PTA甲级考试真题练习23——1023 Have Fun with Numbers
-
PTA甲级考试真题练习5——1005 Spell It Right
-
PTA甲级考试真题练习11——1011 World Cup Betting