Skip to content

Instantly share code, notes, and snippets.

@duyet
Created August 14, 2014 15:06
Show Gist options
  • Save duyet/4d1b9539aef12bde5959 to your computer and use it in GitHub Desktop.
Save duyet/4d1b9539aef12bde5959 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define MAX 1000
int main() {
FILE *input, *output;
int n; // Số lượng phần tử n
int array[MAX]; // Mảng aray chứa n số tiếp theo
input = fopen("input.txt","rt"); // Mở file input
fscanf(input,"%d",&n); // Nhập giá trị n
for (int i = 0; i < n; ++i) {
fscanf(f,"%d",&array[i]); // Lấy n số hạng
}
// Sắp xếp tăng dần
sort(array);
/*
Ta được:: 1, 2, 2, 2, 3, 3, 3, 3, 4
Tìm số lần xuất hiện của mỗi số, bỏ vô 1 cái mảng X
Số 1 xuất hiện 1 lần
Số 2 xuất hiện 3 lần
Số 3 xuất hiện 4 lần
........
*/
int X[MAX];
for (int i = 0; i < MAX; i++) X[i] = 0;
for (int i = 0; i < n; i++) X[array[i]] += 1;
/*
Dùng công thức tính (số * số lần xuất hiện) / n ---> cái nào lớn nhất là nó
*/
float max = 0;
int result = 1;
for (int i = 0; i < MAX; i++) {
float x = (float)(i * X[i]) / n;
if (x >= max) { result = i; max = x; }
}
// OUTPUT
fprintf(output, "%d", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment