[ create a new paste ] login | about

Link: http://codepad.org/u0VBnmhq    [ raw code | output | fork ]

C, pasted on Apr 28:
/*
Ý tưởng: 
Bước 1 : Ta sẽ đi tìm ra tần suất xuất hiện 
nhiều nhất trong mảng
Bước 2 : Lấy tần suất xuất hiện ở bước 1 đem so sánh
với tuần suất xuất hiện của từng phần tử trong mảng,
phần tử nào có tần suất xuất hiện bằng nhau thì
in phần tử đó ra(Lưu ý: Không xuất ra 2 phần tử trùng nhau)
*/


#include<stdio.h>
#include<conio.h>
#define MAX 100

void NhapMang(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("\nNhap vao phan tu a[%d] = ", i);
		scanf_s("%d", &a[i]);
	}
}
void XuatMang(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%4d", a[i]);
	}
}

int TimTanSuatXuatHienMax(int a[], int n)
{
	int Max = 1;  // 1 phần tử tối thiêu xuất hiện
	for (int i = 0; i < n; i++)
	{
		int dem = 1; // Tính chính nó
		for (int j = i + 1; j < n; j++)
		{
			if (a[i] == a[j])
			{
				dem++;
			}
		}
		if (dem > Max)
		{
			Max = dem;
		}
	}
	return Max;
}

int KiemTraBiTrung(int a[], int n, int index)
{
	for (int i = index - 1; i >= 0; i--)
	{
		if (a[i] == a[index])
		{
			return 0; // Bị trùng
		}
	}
	return 1; // Không bị trùng
}

int DemTanSuatXuatHien(int a[], int n, int index)
{
	int dem = 1; // Tính luôn chính nó 
	for (int i = index + 1; i < n; i++)
	{
		if (a[i] == a[index])
		{
			dem++;
		}
	}
	return dem;
}

void LietKeCacSoXuatHienNhieuNhat(int a[], int n)
{
	int TanSuatMax = TimTanSuatXuatHienMax(a, n);
	for (int i = 0; i < n; i++)
	{
		int CheckTrung = KiemTraBiTrung(a, n, i);
		if (CheckTrung == 1)  // chỉ xử lý khi không trùng
		{
			int TanSuat = DemTanSuatXuatHien(a, n, i);
			//printf("\nPhan tu %d xuat hien %d lan", a[i], TanSuat);

			if (TanSuat == TanSuatMax)
			{
				printf("\nPhan tu xuat hien nhieu nhat la %d", a[i]);
			}
		}
	}
	printf("\n=> So lan xuat hien la: %d", TanSuatMax);
}
int main()
{
	int n;
	do
	{
		printf("\nNhap vao so luong phan tu cua mang: ");
		scanf_s("%d", &n);
		if (n < 0 || n > MAX)
		{
			printf("\nSo luong phan tu khong hop le. Xin kiem tra lai !");
		}
	} while (n < 0 || n > MAX);

	int a[MAX];
	NhapMang(a, n);
	XuatMang(a, n);

	/*int TanSuatMax = TimTanSuatXuatHienMax(a, n);
	printf("\nTan suat xuat hien lon nhat la: %d", TanSuatMax);*/

	LietKeCacSoXuatHienNhieuNhat(a, n);

	_getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Line 17: error: conio.h: No such file or directory
In function 'NhapMang':
Line 18: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 26: error: 'for' loop initial declaration used outside C99 mode
In function 'TimTanSuatXuatHienMax':
Line 35: error: 'for' loop initial declaration used outside C99 mode
Line 38: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraBiTrung':
Line 55: error: 'for' loop initial declaration used outside C99 mode
In function 'DemTanSuatXuatHien':
Line 68: error: 'for' loop initial declaration used outside C99 mode
In function 'LietKeCacSoXuatHienNhieuNhat':
Line 81: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: