描述
某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109)。已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。
输入
包含n+1行:
第一行是整数n,表示自然数的个数;
第2~n+1每行一个自然数。
40%的数据满足:1<=n<=1000;
80%的数据满足:1<=n<=50000;
100%的数据满足:1<=n<=200000,每个数均不超过1500 000 000(1.5*10^9)。
输出
包含m行(m为n个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。
样例输入
8 2 4 2 4 5 100 2 100
样例输出
2 3 4 2 5 1 100 2
#include <iostream> #include<bits/stdc++.h> #include <vector> #include <algorithm> #include <cmath> #include<math.h> #include <cstring> #include<string> #include<cstdio> #include<cstdlib> using namespace std; typedef struct Num { long long num; int times; }; Num test[200005]; struct Rule3 { bool operator()(const Num &a1, const Num &a2) { return a1.num < a2.num; } }; int main() { int n; cin >> n; int i; for (i = 0; i < n; ++i) { cin >> test[i].num; test[i].times = 1; } sort(test, test + n, Rule3()); for (int i = 0; i < n; ++i) { if (test[i].num == test[i + 1].num) { test[i + 1].times += test[i].times; } else { cout << test[i].num << ' ' << test[i].times << endl; } } return