欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pat 甲级 1022 Digital Library

程序员文章站 2024-02-17 12:16:58
...

啥都别说了
智障了

看着输入输出觉得很复杂
其实解决了怎么存储,剩下的比较起来很简单
循环就可以了
没用别人那种精巧的方法
直接建结构体存储,没用到map

知识点: vector用法; getline;
C语言没办法对string进行输入输出,用cin cout比较好

写完了一直不过,因为自己忘了输出要查询的信息

#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;


struct bo{

    string id;
    string title;
    string author;
    vector<string> keyword;
    string publisher;
    string year;
};

map<string,vector<string> > keymap;
bo book[11000];

bool cmp(bo a, bo b){
    return a.id < b.id;
}

int main(){

    scanf("%d",&n); getchar();
    for(int i=0; i<n; i++){

        string id, title, author, publisher, key, year;
        getline(cin,id); book[i].id = id; 
        getline(cin,title); book[i].title = title; 
        getline(cin,author); book[i].author = author;
        while(cin >> key){
            book[i].keyword.push_back(key);
            char c = getchar();
            if(c == '\n') break;
        }
        getline(cin,publisher); book[i].publisher = publisher;
        getline(cin,year); book[i].year = year;

    }

    sort(&book[0],&book[n],cmp);
    scanf("%d",&m);
    for(int j=0; j<m; j++){
        int index;
        int flag = 0;
        string in;
        scanf("%d: ",&index);
        getline(cin,in);
        cout << index << ": " << in << endl;        
        switch(index){

            case 1: for(int k=0; k<n; k++){
                      if(book[k].title == in) {
                        cout << book[k].id << endl;
                        flag = 1;
                      }                                 
                    }
                    if(flag == 0) cout << "Not Found" << endl;
                    break;

            case 2: for(int k=0; k<n; k++){
                      if(book[k].author == in) {
                        cout << book[k].id << endl;
                        flag = 1;
                      }                                 
                    }
                    if(flag == 0) cout << "Not Found" << endl;
                    break;

            case 3: for(int k=0; k<n; k++){
                        for(int j=0; j<book[k].keyword.size(); j++){
                            if(book[k].keyword[j] == in) {
                            cout << book[k].id << endl;
                            flag = 1;
                            }       
                        }                                           
                    }
                    if(flag == 0) cout << "Not Found" << endl;
                    break;

            case 4: for(int k=0; k<n; k++){
                      if(book[k].publisher == in) {
                        cout << book[k].id << endl;
                        flag = 1;
                      }                                 
                    }
                    if(flag == 0) cout << "Not Found" << endl;
                    break;

            case 5: for(int k=0; k<n; k++){
                      if(book[k].year == in) {
                        cout << book[k].id << endl;
                        flag = 1;
                      }                                 
                    }
                    if(flag == 0) cout << "Not Found" << endl;
                    break;
        }
    }



    return 0;
}

啊!