我有这件事要做。 N必须小于等于1 000000。每个元素必须在20到50之间,但是我不知道如何做到这一点,还没有做很多有竞争力的编程。 用户输入N个数字。该程序将查找每个元素的频率,然后按降序打印。 我尝试做一个while循环(请参阅main),但是没有用。请帮忙。
输入格式:
9
42 42 34 26 42 35 34 47 47
输出:
42 3
34 2
47 2
26 1
35 1
#include <iostream>
#include <algorithm>
using namespace std;
struct ele
{
int count, index, val;
};
bool mycomp(struct ele a, struct ele b) {
return (a.val < b.val);
}
bool mycomp2(struct ele a, struct ele b) {
if (a.count != b.count) return (a.count < b.count);
else return a.index > b.index;
}
void sortByFrequency(int arr[], int n)
{
struct ele element[n];
for (int i = 0; i < n; i++)
{
element[i].index = i;
element[i].count = 0;
element[i].val = arr[i];
}
stable_sort(element, element + n, mycomp);
element[0].count = 1;
for (int i = 1; i < n; i++)
{
if (element[i].val == element[i - 1].val)
{
element[i].count += element[i - 1].count + 1;
element[i - 1].count = -1;
element[i].index = element[i - 1].index;
}
else element[i].count = 1;
}
stable_sort(element, element + n, mycomp2);
for (int i = n - 1, index = 0; i >= 0; i--)
if (element[i].count != -1)
for (int j = 0; j < element[i].count; j++)
arr[index++] = element[i].val;
}
int main() {
int n; cin >> n;
int* arr = new int[n];
int* seen = new int[n];
for (int i = 0; i < n; i++){
while(arr[i] < 20 || arr[i] > 50)
cin >> arr[i];
}
sortByFrequency(arr, n);
for (int i = 0; i < n; i++) {
if (seen[i] == 0) {
int count = 0;
for (int j = i; j < n; j++)
if (arr[j] == arr[i]) {
count += 1;
seen[j] = 1;
}
cout << arr[i] << " " << count << endl;
}
}
}
答案 0 :(得分:1)
我并不是说竞争性编程有助于提高编程技能。 (至少不是日常业务的编程技能。)但是,我很想动,而且无法抗拒。 (这是星期一的早晨,可能是一周的热身。)
整理评论的想法,我明白了:
Error in `$<-.data.frame`(`*tmp*`, title, value = character(0)) :
replacement has 0 rows, data has 66366
注意:
我使用
Access to XMLHttpRequest at 'https://.../' from origin 'https://...' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
以最少的内存占用存储出现的次数。 (输入值范围的明确限制使我感到鼓舞。)
user4581301使我对内存消耗很敏感。 (在阅读之前,我还没有意识到实际上不需要存储输入值。)
使用#include <cassert>
#include <iostream>
#include <vector>
#include <set>
int main()
{
// input and count frequency
int n; std::cin >> n;
assert(n >= 0 && n < 1000000);
const int valueMin = 20, valueMax = 50;
int freq[valueMax - valueMin + 1] {};
for (int i = 0; i < n; ++i) {
int value; std::cin >> value;
++freq[value - valueMin];
}
// sort frequency
std::set<std::pair<int, int>, bool(*)(const std::pair<int, int>&, const std::pair<int, int>&)>
freqSorted([](const std::pair<int, int> &pair1, const std::pair<int, int> &pair2) {
if (pair1.first != pair2.first) return pair1.first > pair2.first;
return pair1.second < pair2.second;
});
for (int i = valueMin; i <= valueMax; ++i) {
if (const int freqI = freq[i - valueMin]) {
freqSorted.insert(std::make_pair(freqI, i));
}
}
// output
for (std::pair<int, int> entry : freqSorted) {
std::cout << entry.second << ' ' << entry.first << '\n';
}
}
(就像Vishnu Dasu推荐的那样)也是我的第一个想法。在测试时,我一直想知道是否缺少结果,直到我意识到以出现次数为键的const int valueMin = 20, valueMax = 50;
int freq[valueMax - valueMin + 1] {};
只会以相同的频率存储多个值之一。
因此,我将其更改为两个值都为键的std::map
。
输出:
map