The operation of map must be familiar! ! !
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <climits>
using namespace std;
const int MAXN = 1005;
const int INF = INT_MAX;
int main(){
// freopen("in.txt", "r", stdin);
int N, sample, score;
while(~scanf("%d", &N)){
if(N == 0) break;
map<int, int> mymap;
for(int i = 0; i < N; i++){
scanf("%d", &score);
map<int, int>::iterator it;
it = mymap.find(score);
if(it != mymap.end()){//找到
it->second++;
}
else mymap[score] = 1;
}
scanf("%d", &sample);
map<int, int>::iterator it;
it = mymap.find(sample);
if(it == mymap.end()) printf("0\n");
else printf("%d\n", it->second);
}
return 0;
}
Continue to be familiar with the operation of map, to be clear about rbegin () and rend ()
This question also uses the self-sorting function of map.
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <climits>
using namespace std;
const int MAXN = 1005;
const int INF = INT_MAX;
int main(){
// freopen("in.txt", "r", stdin);
int M;
string s1, s2, s3;
while(~scanf("%d", &M)){
map<string, string> come;
map<string, string> leave;
for(int i = 0; i < M; i++){
cin >> s1 >> s2 >> s3;
come[s2] = s1;
leave[s3] = s1;
};
cout << come.begin()->second << " " << leave.rbegin()->second << endl;
}
return 0;
}