- 题解
2025年三月模拟赛 T1
- @ 2025-4-2 16:59:46
无map,无双指针做法
思路:
排序后的数组中不存在,,的情况,所以只需要判断当前下标与上一标是否相等,相等cnt++即可,如果不一样输出当前下标对应的数值和上一数值所对应的cnt
#include <iostream>
#include <algorithm>
using namespace std;
int a[200010];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
int cnt = 1;
for (int i = 0; i < n; i++)
{
if (i == 0)
cout << a[0] <<' ';
else if (a[i] != a[i - 1]) //1 1 2
{
cout << cnt << endl << a[i] << ' ';
cnt = 1;
}
else
cnt ++;
}
cout << cnt;
return 0;
}
0 comments
No comments so far...