golang map
Posted 学无止境,每天进步一小步
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang map相关的知识,希望对你有一定的参考价值。
Our friend Monk has been made teacher for the day today by his school professors . He is going to teach informatics to his colleagues as that is his favorite subject . Before entering the class, Monk realized that he does not remember the names of all his colleagues clearly . He thinks this will cause problems and will not allow him to teach the class well. However, Monk remembers the roll number of all his colleagues very well . Monk now wants you to help him out. He will initially give you a list indicating the name and roll number of all his colleagues. When he enters the class he will give you the roll number of any of his colleagues belonging to the class. You need to revert to him with the name of that colleague.
Input Format
The first line contains a single integers NN denoting the number of Monk‘s colleagues. Each of the next NN lines contains an integer and a String denoting the roll number and name of the ii th colleague of Monk. The next Line contains a single integer qq denoting the number of queries Monk shall present to you when he starts teaching in class. Each of the next qq lines contains a single integer xx denoting the roll number of the student whose name Monk wants to know.
Output Format
You need to print qq Strings, each String on a new line, indicating the answers to each of Monk‘s queries.
Constrains
1≤N≤1051≤N≤105
1≤RollNumber≤1091≤RollNumber≤109
1≤|Name|≤251≤|Name|≤25
1≤q≤1041≤q≤104
1≤x≤1091≤x≤109
Note
The name of each student shall consist of lowercase English alphabets only. It is guaranteed that the roll number appearing in each query shall belong to some student from the class.
其实就是用到了golang 数据集合map,貌似Map的性能不是太好
package main import "fmt" func main() { //fmt.Println("Hello World!") mapDic := make(map[int]string) var mapCount int fmt.Scanln(&mapCount) var key int var value string for i:=0;i<mapCount;i++{ fmt.Scanln(&key, &value) mapDic[key] = value } var searchCount int fmt.Scanln(&searchCount) var searchKey int for j:=0;j<searchCount;j++{ fmt.Scanln(&searchKey) fmt.Println(mapDic[searchKey]) } }
以上是关于golang map的主要内容,如果未能解决你的问题,请参考以下文章
golang goroutine例子[golang并发代码片段]