zoukankan      html  css  js  c++  java
  • PAT A1022 Digital Library (30分)



    题型分类:map
    题目大意:输入一本书的各个数据,建立图书查询系统,最后对书目进行查询。
    解题思路:使用map来映射书和书的各个数据,用set来存储书的编号,set可以自动去重并且按从小到大的顺序排列
    注意
    1.由于id是一个七位数字,在用scanf("%d",&id)读入是0001111会变成1111即自动完成了去零过程,所以输出时需要进行printf("%07d",id)即完成补0操作
    2.getchar可以读入换行符防止getline读入一个已经结束的一行此时得到的是一个空数组;
    3.尽量把查询的东西设置为一样的方便使用同样的query函数进行查询,在此题中需要使用引用来读入是为了减少数据复制的时间,防止最后一组数据超时;

    #include <cstdio>
    #include <map>
    #include <set>
    #include <iostream>
    #include <string>
    using namespace std;
    map<string , set<int> > mpTitle,mpAuthor,mpKey,mpPub,mpYear;
    
    void query(map<string ,set<int> > &mp, string &str){//不使用引用容易超时
        if(mp.find(str) == mp.end()) printf("Not Found
    ");
        else{
            for(set<int>::iterator it = mp[str].begin();it!=mp[str].end();it++){
                printf("%07d
    ",*it);
            }
        }
    }
    
    int main(){
        int n,m,id,type;
        string title,author,key,pub,year;
        scanf("%d",&n);
        for(int i = 0;i<n;i++){
            scanf("%d",&id);
            char c = getchar();//接收掉id后面的换行
            getline(cin,title);
            mpTitle[title].insert(id);
            getline(cin,author);
            mpAuthor[author].insert(id);
            while(cin >> key){
               mpKey[key].insert(id);
               c = getchar();
                if(c == '
    ') break;
           }
            getline(cin,pub);
            mpPub[pub].insert(id);
            getline(cin,year);
            mpYear[year].insert(id);        
        }
        
        string temp;
        scanf("%d",&m);
        for(int i =0;i<m;i++){
            scanf("%d: ",&type);
            getline(cin,temp);
            cout<<type<<": "<<temp<<endl;
            if(type==1) query(mpTitle,temp);
            else if(type==2) query(mpAuthor,temp);
            else if(type==3) query(mpKey,temp);
            else if(type==4) query(mpPub,temp);
            else query(mpYear,temp);
        }
        return 0;
    }
    
  • 相关阅读:
    二分类下的混淆矩阵
    多项式的回归
    使用变换来提升单回归准确度的一个反例
    使用OpenOffice.org将各类文档转为PDF
    22 Best Sites To Download Free Sprites
    给Libgdx的ShapeRenderer开启抗锯齿
    How get a String Width in Libgdx?
    JS实现IE下打印和打印预览
    spring-mvc不拦截静态资源的配置
    This Android SDK requires Android Developer Toolkit version 23.0.0 or above
  • 原文地址:https://www.cnblogs.com/shuibeng/p/13587405.html
Copyright © 2011-2022 走看看