zoukankan      html  css  js  c++  java
  • [PAT] A1022 Digital Library

    【题目大意】

    给出几本书的信息,包括编号,名字,出版社,作者,出版年份,关键字;然后给出几个请求,分别按照1->名字,2->出版社等对应信息查询符合要求的书的编号。

    【思路】

    模拟。

    【坑】

    1) 根据空格判断关键字key,遇到空格存下前一个key,则到最后一个key没有保存,退出while循环后要再存一次。

    2) 漏根据id排序

    3) Not Found 拼写错了...

    4) 题目说年份在[1000, 3000],但是并不是这样的!输出年份一定要保留4位高位填充0补齐,否则测试点1过不了。这个地方我调试了好久,气死人了!

    【tips】

    1) 输入带空格的string

    方法一:

      string s;

      getline(cin, s);

    方法二:

      string s;

      char c;

      while((c=cin.get())!=' ')

        s = s + c;

    2) 题目中根据id排序,我的处理方法是用集合set存储符合查询条件的id,循环结束后一次输出set里的id值即可,因为set会自动帮你排序。(时间3ms;4ms;4ms;4ms;280ms)

    也可以先调用sort排序,后续依次比对输出的时候自然就是按顺序的了,这样会慢一些。(时间5ms;5ms;5ms;5ms;393ms)

    3) 网上有人说用map方便,或许今后可以试试?

    【AC代码】

      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include<iostream>
      3 #include<queue>
      4 #include<vector>
      5 #include<set>
      6 #include<string>
      7 #include <algorithm>
      8 using namespace std;
      9 #define N 10002
     10 struct book {
     11     string id;
     12     string title, author;
     13     vector<string> key;
     14     string publisher;
     15     int year;
     16 };
     17 vector<book> info;
     18 int main()
     19 {
     20     int n;
     21     cin >> n;
     22     int i;
     23     for (i = 0; i < n; i++)
     24     {
     25         book tbook;
     26         cin >> tbook.id;
     27         char c;
     28         c = cin.get();
     29         getline(cin,tbook.title);
     30         getline(cin, tbook.author);
     31         string str;
     32 
     33         while (cin >> str) {
     34             tbook.key.push_back(str);
     35             if (getchar() == '
    ')
     36                 break;
     37         }
     38         /*
     39         while ((c = cin.get()) != '
    ')
     40         {
     41             if (c == ' '){
     42                 tbook.key.push_back(str);
     43                 str.clear();
     44             }
     45             else{
     46                 str += c;
     47             }
     48         }
     49         tbook.key.push_back(str);
     50         */
     51         getline(cin, tbook.publisher);
     52         cin >> tbook.year;
     53         info.push_back(tbook);
     54     }
     55     int m, op;
     56     cin >> m;
     57     for (i = 0; i < m; i++)
     58     {
     59         scanf("%d: ", &op);
     60         if (op == 1) {
     61             set<string>ans_id;
     62             string ttitle;
     63             getline(cin, ttitle);
     64             cout << "1: " << ttitle << endl;
     65             int j;
     66             bool find = false;
     67             for (j = 0; j < info.size(); j++)
     68                 if (ttitle == info[j].title)
     69                 {
     70                     find = true;
     71                     ans_id.insert(info[j].id);
     72                     //cout << info[j].id << endl;
     73                 }
     74             if (!find)
     75                 cout << "Not Found" << endl;
     76             else {
     77                 set<string>::iterator it = ans_id.begin();
     78                 for (; it != ans_id.end(); it++)
     79                     cout << *it << endl;
     80             }
     81         }
     82         if (op == 2) {
     83             set<string>ans_id;
     84             string tauthor;
     85             getline(cin, tauthor);
     86             cout << "2: " << tauthor << endl;
     87             int j;
     88             bool find = false;
     89             for (j = 0; j < info.size(); j++)
     90                 if (tauthor == info[j].author)
     91                 {
     92                     find = true;
     93                     ans_id.insert(info[j].id);
     94                     //cout << info[j].id << endl;
     95                 }
     96             if (!find)
     97                 cout << "Not Found" << endl;
     98             else {
     99                 set<string>::iterator it = ans_id.begin();
    100                 for (; it != ans_id.end(); it++)
    101                     cout << *it << endl;
    102             }
    103         }
    104         if (op == 3) {
    105             set<string>ans_id;
    106             string tkey;
    107             getline(cin, tkey);
    108             cout << "3: " << tkey << endl;
    109             int j;
    110             bool find = false;
    111             for (j = 0; j < info.size(); j++)
    112                 for (int k = 0; k < info[j].key.size(); k++)
    113                 {
    114                     if (tkey == info[j].key[k])
    115                     {
    116                         find = true;
    117                         ans_id.insert(info[j].id);
    118                         //cout << info[j].id << endl;
    119                         break;
    120                     }
    121                 }
    122             if (!find)
    123                 cout << "Not Found" << endl;
    124             else {
    125                 set<string>::iterator it = ans_id.begin();
    126                 for (; it != ans_id.end(); it++)
    127                     cout << *it << endl;
    128             }
    129         }
    130         if (op == 4) {
    131             set<string>ans_id;
    132             string tpub;
    133             getline(cin, tpub);
    134             cout << "4: " << tpub << endl;
    135             int j;
    136             bool find = false;
    137             for (j = 0; j < info.size(); j++)
    138                 if (tpub == info[j].publisher)
    139                 {
    140                     find = true;
    141                     ans_id.insert(info[j].id);
    142                     //cout << info[j].id << endl;
    143                 }
    144             if (!find)
    145                 cout << "Not Found" << endl;
    146             else {
    147                 set<string>::iterator it = ans_id.begin();
    148                 for (; it != ans_id.end(); it++)
    149                     cout << *it << endl;
    150             }
    151         }
    152         if (op == 5) {
    153             set<string>ans_id;
    154             int tyear;
    155             cin >> tyear;
    156             //cout << "5: " << tyear << endl;
    157             printf("5: %04d
    ", tyear);
    158             int j;
    159             bool find = false;
    160             for (j = 0; j < info.size(); j++)
    161                 if (tyear == info[j].year)
    162                 {
    163                     find = true;
    164                     ans_id.insert(info[j].id);
    165                     //cout << info[j].id << endl;
    166                 }
    167             if (!find)
    168                 cout << "Not Found" << endl;
    169             else {
    170                 set<string>::iterator it = ans_id.begin();
    171                 for (; it != ans_id.end(); it++)
    172                     cout << *it << endl;
    173             }
    174         }
    175     }
    176     /*for (i = 0; i < info.size(); i++)
    177     {
    178         cout << info[i].id << endl;
    179         cout << info[i].title << endl;
    180         cout << info[i].author << endl;
    181         for (int j = 0; j < info[i].key.size(); j++)
    182             cout << info[i].key[j] << endl;
    183         cout << endl;
    184         cout << info[i].publisher << endl;
    185         cout << info[i].year << endl;
    186     }*/
    187     return 0;
    188 }
  • 相关阅读:
    Java设计模式之工厂模式的两种实现方式
    1.揭开消息中间件RabbitMQ的神秘面纱
    再谈spring的循环依赖是怎么造成的?
    深入理解java反射原理
    关于springmvc的helloworld的压测报告
    线程池工厂Executors编程的艺术
    lazy-init 懒加载的艺术
    spring中的mybatis的sqlSession是如何做到线程隔离的?
    java并发机制锁的类型和实现
    ReentrantReadWriteLock 读写锁解析
  • 原文地址:https://www.cnblogs.com/yue36/p/12364584.html
Copyright © 2011-2022 走看看