map、set 使用方法 | 1022 图书馆信息查询

看了答案才知道了这题的各种骚操作,然后敲了一顿骚键盘,然后wa。调了很久,才发现要规格化打印……mdzz……

注:加粗代码为傻逼规格化打印代码:

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>


#define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 1010
#define MAX (1<<30)-1
#define V vector<int>

#define MAP map<string,set<int> > //将名字映射为ID 

using namespace std;

MAP mTitle,mAuthor,mKey,mPublish,mYear;

void query(MAP & mp,string obj) {
    set<int> s=mp[obj];
    if(!s.size()) {
        puts("Not Found");
        return;
    }
    set<int>::iterator it=s.begin();
    while(it!=s.end()) {
        O("%07d
",*it);
        it++;
    }
}

int main() {
//    freopen("1022.txt","r",stdin);
    int N,id,K,op;
    I("%d",&N) ;
    char buf[1000];
    while(N--) {
        //ID
        I("%d",&id);
        getchar();//处理空格
        //书名
        gets(buf);
        mTitle[string(buf)].insert(id);
        //作者
        gets(buf);
        mAuthor[string(buf)].insert(id);
        //关键词
        while(I("%s",buf))  {
            mKey[string(buf)].insert(id);
            if(getchar()=='
') break;
        }
        //出版商
        gets(buf);
        mPublish[string(buf)].insert(id);
        //
        gets(buf);
        mYear[string(buf)].insert(id);
    }
    I("%d",&K) ;
    while(K--) {
        I("%d: ",&op);
        gets(buf);
        O("%d: %s
",op,buf);
        switch(op) {
            case 1:
                query(mTitle,string(buf));
                break;
            case 2:
                query(mAuthor,string(buf));
                break;
            case 3:
                query(mKey,string(buf));
                break;
            case 4:
                query(mPublish,string(buf));
                break;
            case 5:
                query(mYear,string(buf));
                break;
        }
    }
    return 0;
}

今日不宜编程……

原文地址:https://www.cnblogs.com/TQCAI/p/8585515.html