題目描述:
n(n<=200000)個數(1.5*10^9范圍內),輸出重復的數(最多10000個)出現的次數
代碼:
#include <iostream> #include <map> #include <cstdio> using namespace std; map<int,int> a; int n,t; int main() { freopen("count.in","r",stdin); //打開輸入文件 freopen("count.out","w",stdout); //打開輸出文件 cin>>n; for(int i=1;i<=n;i++) { cin>>t; a[t]++; } for(map<int,int>::iterator it=a.begin();it!=a.end();it++) cout<<it->first<<" "<<it->second<<" "<<endl; fclose(stdin);//關閉輸入文件 fclose(stdout);//關閉輸出文件 }
幾個操作總結:
1.定義map<數據類型,數據類型> 變量名;
2.首元素:x.began()
3.尾元素:x.end()
4.遍歷:
先定義一個迭代器:
map<數據類型,數據類型>::iterater it;
接着:
it=a.begin();it!=a.end();it++
這里it相當於指針.
4.輸出元素:
定義的第一個:it->first
第二個:it->second