1486. 统计数字(先排序,再遍历数组统计出现次数)

来源:互联网 发布:java课程设计 编辑:程序博客网 时间:2024/05/28 18:44
/*1486. 统计数字  给一串数字,统计各个数字出现的次数  若直接用map会超时。先从小到大排序,然后遍历数据依次统计各个数字出现的次数  */#include <iostream>#include <stdlib.h>#include <stdio.h>#include <algorithm>using namespace std;int main(){    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    long long m[200000];    int n;    bool ok = false;    while(scanf("%d", &n) != EOF) {       if(ok)         printf("\n");       ok =true;       for(int i=0; i<n ; i++){          scanf("%lld", &m[i]);         }                 sort(m, m+n);       for(int i=0; i<n;)                                    {        int j=i;      while(j<n && m[i] == m[j])              j++;          //cout << m[i] << " "<< (j-i) << endl;           int l = j-i;          printf("%lld ",  m[i]);          printf("%d\n", l);          i = j;   }    //printf("\n");    }  //  system("pause");    return 0;}

原创粉丝点击