Другие языки программирования и технологии

Напишите задачу на C++. Найти, какая оценка наиболее часто встречалась на ЕГЭ. Входные данные: 1 1 1 2 2 3 1 3 4 5 3.

Вот пример в C++:

#include <stdio.h>
#include <stdlib.h>
#include <map>
using namespace std;
#define uint unsigned int
int main(void) {
uint countMarks = 0;
map <uint, uint> listMarks;
printf("Введите количество полученных оценок на ЕГЭ: ");
scanf("%u", &countMarks);
for (uint i = 0; i < countMarks; i++) {
uint mark = 0;
printf("Введите %u-ую оценку: ", i+1);
scanf("%u", &mark);
if (listMarks.find(mark) != listMarks.end())
++listMarks[mark];
else
listMarks[mark] = 1;
}
uint MaxCount = 0;
map <uint, uint>::iterator markFromList;
for (markFromList = listMarks.begin(); markFromList != listMarks.end(); markFromList++)
if (markFromList->second > MaxCount)
MaxCount = markFromList->second;
for (markFromList = listMarks.begin(); markFromList != listMarks.end(); markFromList++)
if(markFromList->second == MaxCount)
printf("Оценка (%u) встречается %u раз (а) \n", markFromList->first, markFromList->second);
system("pause");
return 0;
}

Александр Савченко
Александр Савченко
2 069
Лучший ответ
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<unsigned short> estimates = { 1, 1, 1, 2, 2, 3, 1, 3, 4, 5, 3 };
size_t size = estimates.size();
const size_t buffer = 6;
vector<int> statistics(buffer);
statistics.at(0) = size;
for (size_t i = 0; i < size; ++i) {
switch (estimates[i]) {
case 1: ++statistics.at(1); break;
case 2: ++statistics.at(2); break;
case 3: ++statistics.at(3); break;
case 4: ++statistics.at(4); break;
case 5: ++statistics.at(5); break;
}
}
int mx = *max_element(statistics.begin() + 1, statistics.end());
for (size_t i = 1; i < buffer; ++i) if (statistics.at(i) == mx) cout << i << endl;
cin.sync();
cin.get();
}
Считаем частоту повторения...
Иван Говорков
Иван Говорков
54 409

Похожие вопросы