考研机试 | C++ | 王道复试班 | map专场

关于map

在这里插入图片描述
map的一些基本操作:

  1. 判空 :map.empty()
  2. 键值对的个数: map.size()
  3. 插入
    在这里插入图片描述
  4. 删除 erase
    在这里插入图片描述
  5. 查找某个键是否存在 find方法
    在这里插入图片描述
  6. 遍历map
    在这里插入图片描述

查找学生信息(清华上机)

题目

输入N个学生的信息,然后进行查询。
输入描述:

输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息格式如下:
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:
02
03
01
04

输出描述:

输出M行,每行包括一个对应于查询的学生的信息。 如果没有对应的学生信息,则输出“No Answer!”

代码:

#include <cstdio>
#include <string>
#include <map>

using namespace std;

struct Student{
    
    
	string name;
	string gender;
	int age;
};

int main(){
    
    
	int n;
	scanf("%d",&n);
	map<string,Student> infomap;
	// 录入信息 
	for(int i=0;i<n;i++){
    
    
		char num[30];
		char name[30];
		char gender[30];
		int age;
		scanf("%s%s%s%d",num,name,gender,&age);
		// key string
		string numstr = num;
		Student student;
		student.name = name;
		student.gender = gender;
		student.age = age;
		 
		infomap[numstr] = student;
	}
	// 开始查找
	int m;
	scanf("%d",&m);
	for(int i=0;i<m;i++){
    
    
		char num[30];
		scanf("%s",num);
		string numstr = num; //转化为C++格式的字符串 
	
		if (infomap.find(numstr)!=infomap.end()){
    
    
			//找到了键值对
			printf("%s %s %s %d\n",numstr.c_str(),
			infomap[numstr].name.c_str(),
			infomap[numstr].gender.c_str(),
			infomap[numstr].age); 
		}
		else{
    
    
			printf("No Answer!\n");
		}
	}
}

魔咒词典(浙大机试)

题目:

描述
哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。 给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”

输入描述:
首先列出词典中不超过100000条不同的魔咒词条,每条格式为: [魔咒] 对应功能 其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。 词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。

输出描述:
每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”

示例:

输入:

[expelliarmus] the disarming charm
[rictusempra] send a jet of silver light to hit the enemy
[tarantallegra] control the movement of one's legs
[serpensortia] shoot a snake out of the end of one's wand
[lumos] light the wand
[obliviate] the memory charm
[expecto patronum] send a Patronus to the dementors
[accio] the summoning charm
@END@
4
[lumos]
the summoning charm
[arha]
take me to the sky

输出:

light the wand
accio
what?
what?

代码

#include <cstdio>
#include <string>
#include <map>

using namespace std;

//substr 左右都是闭区间

int main() {
    
    
    map<string, string> dict;
    // 构建词典
    while (true) {
    
    
        // 输入一整行词条
        char line[200];
        fgets(line, 200, stdin);
        string linestr = line; // c-->c++
        linestr.pop_back(); // 去掉末尾的\n
        // 如果是 @END@ 结尾就退出
        if (linestr == "@END@") break;

        // 准备切割字符串
        string word = linestr.substr(0, linestr.find(']') + 1);
        string info = linestr.substr(linestr.find(']') + 2); //默认截取到最后
        //printf("word = %s,info=%s\n",word.c_str(),info.c_str()); // 测试切割

        // 插入到字典中
        dict[word] = info;
        dict[info] = word;
    }
    int n;
    scanf("%d", &n);
    getchar();
    for (int i = 0; i < n; i++) {
    
    
        char line[200];
        fgets(line, 200, stdin);
        string linestr = line;
        linestr.pop_back();

        if (dict.find(linestr) != dict.end()) {
    
    
            // 根据魔咒找功能
            if (linestr[0] == '[') {
    
    
                printf("%s\n", dict[linestr].c_str());
            }
            // 根据功能找魔咒
            else {
    
    
                string temp = dict[linestr].substr(1, dict[linestr].find(']') - 1);
                printf("%s\n", temp.c_str());
            }
        } else {
    
    
            printf("what?\n");
        }
    }


}


英语复试常用话题

在这里插入图片描述
学术套话 paper book

I’ve read a paper/book entitled “Scikit-Learn,keras and tensorflow”. It involves some basic knowledge about deep learning, such as CNN, RNN and LSTM. It teaches me how to fit a better complete process. I hope to have a better understanding of it in my future study.

为什么报考我们所

first of all,the school is a famous graduate school,for another I hope to have a better understanding of it in my future study. for example,in my study life,…如上,but i have a bit confuse about it,i want to further study。

接话:

(1)thank you for your question 感谢您的提问
(2)well,actually,speaking of this topic,i do have some ideas about this 谈到这个话题,我有一些想法

没听懂:

I’m sorry. I didn’t get it/that.
抱歉老师,我没get到
Could you please say it in another way?
您能否换种方式来询问这个问题

你能介绍一下你的大学生活嘛? (介绍一下学校)

Like I said before,I am a senior student in Qingdao University of Science and Technology , majoring in computer science and technology.I enjoy most is the school facilities,such as the library and the labs, as well as the professors who share some useful knowledge with us.There are many books in our library,such as the book named ‘Hands-On Machine Learning with Scikit-Learn,keras and tensorflow’,In short, I think my school has trained me,it is a good school。

猜你喜欢

转载自blog.csdn.net/lijiamingccc/article/details/129235280
今日推荐