マップの操作に慣れている必要があります!!!
#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;
}
rbegin()およびrend()関数について明確にするために、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;
}