PAT甲刷题——U4.2

注意:允许转载,但转载请注明作者和出处

1499. 数字图书馆

题目

在这里插入图片描述

答案

#include <iostream>
#include <vector>
#include <cstring>
#include <sstream>
#include <set>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, m;
struct Book
{
    string id, name, author;
    set<string> keywords;
    string publisher;
    int year;
}books[N];
int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
        string id, name, author;
        cin >> id;
        getchar();
        getline(cin, name), getline(cin, author);
        
        string line;
        string keyword;
        set<string> keywords;
        getline(cin, line);
        stringstream ssin(line);
        while(ssin >> keyword) keywords.insert(keyword);
        
        string publisher;
        getline(cin, publisher);
        
        int year;
        cin >> year;
        
        books[i] = {id, name, author, keywords, publisher, year};
    }
    // 经下方code检验,上述操作无误
    // for (int i = 0; i < n; i ++)
    // {
    //     Book book = books[i];
    //     cout << book.id << ' ' << book.name << ' ' << book.author << endl;
    //     for (auto k : book.keywords) cout << k << ' ';
    //     puts("");
    //     cout << book.publisher << ' ' << book.year << endl;
    // }
    cin >> m;
    while (m --)
    {
        // scanf输入字符串的时候,遇见空格就不读了
        // scanf内容参考链接:
        // https://blog.youkuaiyun.com/m0_46450606/article/details/137961671
        int num;
        char str[100];
        scanf("%d: %[^\n]", &num, str);
        vector<string> res;
        // cout << num << ' ';
        // for (int i = 0; i < 100; i ++) cout << str[i];
        // 找到书名在结构体数组中出现的时对应的id
        if (num == 1)
        {
            for (int i = 0; i < n; i ++)
                if (books[i].name == str)
                    res.push_back(books[i].id);
        }
        else if (num == 2)
        {
            for (int i = 0; i < n; i ++)
                if (books[i].author == str)
                    res.push_back(books[i].id);
        }
        else if (num == 3)
        {
            for (int i = 0; i < n; i ++)
                if (books[i].keywords.count(str))
                    res.push_back(books[i].id);
        }
        else if (num == 4)
        {
            for (int i = 0; i < n; i ++)
                if (books[i].publisher == str)
                    res.push_back(books[i].id);
        }
        else
        {
            int pub_year = stoi(str);
            for (int i = 0; i < n; i ++)
                if (books[i].year == pub_year)
                    res.push_back(books[i].id);
        }
        printf("%d: %s\n", num, str);
        if (res.empty()) puts("Not Found");
        else
        {
            sort(res.begin(), res.end());
            for (auto r : res) cout << r << endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值