P1097 统计数字 洛谷(map)

直接用map容器建立对应关系水过…首先别忘记清空整个map容器,最后输入键使对应的值+1就好了。代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <utility>
#define ll long long
#define ull_ unsigned long long

using namespace std ;

int main(){
    int n ;
    cin >> n ;
    map<int , int> map_ ;
    map_.clear() ;
    for ( int i = 0 ; i < n ; i ++ ){
        int x ;
        cin >> x ;
        map_[x] ++ ;
    }
    map<int , int>::iterator it ;
    for ( it = map_.begin() ; it != map_.end() ; it ++ ){
        cout << it->first << " " << it->second << endl ;
    }
    return 0 ;
}
posted @ 2018-10-09 22:11  Cantredo  阅读(333)  评论(0编辑  收藏  举报