[ create a new paste ] login | about

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

C, pasted on Oct 15:
#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 8: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 16: error: 'for' loop initial declaration used outside C99 mode
In function 'TimTanSuatXuatHienMax':
Line 25: error: 'for' loop initial declaration used outside C99 mode
Line 28: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraBiTrung':
Line 45: error: 'for' loop initial declaration used outside C99 mode
In function 'DemTanSuatXuatHien':
Line 58: error: 'for' loop initial declaration used outside C99 mode
In function 'LietKeCacSoXuatHienNhieuNhat':
Line 71: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: