-
Day 0: Mean, Median, and Mode해커랭크(HackerRank) 2018. 8. 19. 17:40반응형
1. 문제
2. 알고리즘
키워드 - 구현
3. 코드
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556#include <cmath>#include <cstdio>#include <vector>#include <iostream>#include <algorithm>using namespace std;int getMode(const vector<int> &array) {int mode = 0;int size = array.size();int count = 0, max = 0;vector<int> copy (array.size());copy = array;sort (copy.begin(), copy.end());int current = 0;for(int i = 0;i < size;i++) {if (copy[i] == current) {count++;} else {count = 1;current = copy[i];}if (count > max) {max = count;mode = copy[i];}}return mode;}int main() {/* Enter your code here. Read input from STDIN. Print output to STDOUT */int n; cin >> n;vector<int> arr(n);for(int i=0; i<n; i ++)cin >> arr[i];sort(arr.begin(), arr.end());int sum = 0;for(int i =0; i<n; i++)sum += arr[i];double mean = (double)sum / n;cout << mean << endl;double median = (double)(arr[n/2] + arr[n/2-1]) / 2;cout << median << endl;int mode = getMode(arr);cout << mode << endl;return 0;}cs 반응형'해커랭크(HackerRank)' 카테고리의 다른 글
Time Conversion (0) 2018.08.19 Grading Students (0) 2018.08.19 Breaking the Records (0) 2018.08.19 Birthday Chocolate (0) 2018.08.19 Divisible Sum Pairs (0) 2018.08.19