[ create a new paste ] login | about

Link: http://codepad.org/A6gXnKNu    [ raw code | output | fork | 2 comments ]

C, pasted on Nov 2:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 100
void NhapMang(int a[][MAX], int &dong, int &cot)
{
	//Nhập số dòng
	do
	{
		printf("\nNhap vao so dong: ");
		// Cách tà đạo: scanf("dong =%d",&dong);  // Lúc nhập phải viết thêm  chữ ( dong =  ) ở khung console
		scanf("%d",&dong);

		if(dong < 1 || dong > MAX)
		{
			printf("\nSo dong khong hop le. Xin kiem tra lai!");
		}

	}while(dong < 1 || dong > MAX);

	//Nhập số cột
	do
	{
		printf("\nNhap vao so cot: ");
		scanf("%d",&cot);

		if(cot < 1 || cot > MAX)
		{
			printf("\nSo cot khong hop le. Xin kiem tra lai!");

		}

	}while(cot < 1 || cot > MAX);
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			printf("\nNhap a[%d][%d] = ", i, j);
			scanf("%d", &a[i][j]);
		}
	}
}

void XuatMang(int a[][MAX], int dong, int cot)
{
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			printf("%4d", a[i][j]);
		}
		printf("\n\n");
	}
}
/*
- Kiểm tra phần tử đó có lớn nhất trên dòng nó đang đứng hay không ?
- Kiểm tra phần tử đó có lớn nhất trên cột nó đang đứng hay không ?
- Kiểm tra phần tử đó có lớn nhất trên 2 đường chéo hay không (Đây là hàm khá phức tạp). Trường hợp này mình sẽ cho duyệt theo 4 hướng (4 while):
+ từ vị trí phần tử hiện tại, i--, j-- (giảm qua trái)
+ từ vị trí phần tử hiện tại i--,j++ (giảm phải)
+ // i++,j++ (tăng phải)
+ // i++,j-- (tăng trái)

Sau khi thực hiện 3 hàm trên, duyệt ma trận sẽ tìm được phần tử "Hoàng Hậu"
*/
bool KiemTraCoPhaiPhanTuHoangHau(int a[][MAX], int vtdong, int vtcot, int dong, int cot)
{
	int x = a[vtdong][vtcot];

	// kiểm tra dòng
	for(int i = 0; i < cot; i++)
	{
		if(a[vtdong][i] > x)
		{
			return false;
		}
	}
	// kiểm tra cột
	for(int j = 0; j < dong; j++)
	{
		if(a[j][vtcot] > x)
		{
			return false;
		}
	}
	//ktra duong cheo thu nhat
	int vtdong1 = vtdong + 1;
	int vtcot1 = vtcot + 1;
	while (vtcot1 + 1 < cot && vtdong1 < vtdong)
	{
		if (a[vtcot1][vtdong1] > x)
			return false;
		// tăng phải
		vtcot1++;
		vtdong1++;
	}
	vtdong1 = vtdong - 1;
	vtcot1 = vtcot - 1;
	while (vtcot1 - 1 >= 0 && vtdong1 >= 0)
	{
		if (a[vtcot1][vtdong1] > x)
			return false;
		//giảm qua trái
		vtcot1--;
		vtdong1--;
	}
	// duong cheo thu 2
	vtdong1 = vtdong + 1;
	vtcot1 = vtcot - 1;
	while (vtcot1 - 1 >= 0 && vtdong1 < dong)
	{
		if (a[vtcot1][vtdong1] > x)
			return false;
		// tăng trái
		vtdong1++;
		vtcot1--;
	}
	vtdong1 = vtdong - 1;
	vtcot1 = vtcot + 1;

	while (vtdong1 - 1 >= 0 && vtcot1 < cot)
	{
		if (a[vtcot1][vtdong1] > x)
			return false;
		// giảm phải
		vtdong1--;
		vtcot1++;
	}
	return true;
}

int DemSoLuongPhanTuHoangHau(int a[][MAX], int dong, int cot)
{
	int dem = 0;
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			if(KiemTraCoPhaiPhanTuHoangHau(a, i, j, dong, cot) == true)
			{
				dem++;
			}
		}
	}
	return dem;
}
int main()
{
	int a[MAX][MAX], dong, cot;
	NhapMang(a, dong, cot);
	XuatMang(a, dong, cot);

	int dem = DemSoLuongPhanTuHoangHau(a, dong, cot);
	printf("\nSo luong phan tu hoang hau = %d", dem);
	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
Line 17: error: conio.h: No such file or directory
Line 5: error: expected ';', ',' or ')' before '&' token
In function 'XuatMang':
Line 46: error: 'for' loop initial declaration used outside C99 mode
Line 48: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 66: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'KiemTraCoPhaiPhanTuHoangHau'
In function 'DemSoLuongPhanTuHoangHau':
Line 135: error: 'for' loop initial declaration used outside C99 mode
Line 137: error: 'for' loop initial declaration used outside C99 mode
Line 139: error: 'true' undeclared (first use in this function)
Line 139: error: (Each undeclared identifier is reported only once
Line 139: error: for each function it appears in.)


Create a new paste based on this one


Comments:
posted by nvhuy3 on Mar 16
WRONG CODE!
TEST FOR:
9 11 12 13
4 1 35 14
5 7 2 15
6 8 10 3
RESULT WITH THIS CODE:
35 AND 15 -> COUNT = 2
reply
posted by Nguyen_Van_Quan on Mar 23
/*--------------BÀI TẬP NÂNG CAO--------------
18 / Liệt kê các cột có nhiều "chữ số" nhất trong ma trận.
19 / Cho ma trận các số nguyên A(m x n).Hãy xây dựng ma trận B(m x n) từ ma trận A
sao cho B[i][j] = lớn nhất của dòng i và cột j trong ma trận A.
20 / Đếm số lượng giá trị "hoàng hậu" trên ma trận.Một phần tử được gọi là hoàng hậu
khi nó lớn nhất trên dòng, trên cột và hai đường chéo đi qua nó.*/

/*--------------------------Code by Keyboard Hero--------------------------------*/

#include<iostream>
using namespace std;
int a[100][100];
int n = 0, m = 0;
void Cau_18() {
int dem[100]; //m+1
int max = 0;
for (int j = 0; j < m; j++) {
dem[j] = 0;
for (int i = 0; i < n; i++) {
while (a[i][j]) {
a[i][j] /= 10;
dem[j]++;
}
}
if (dem[j] > max) {
max = dem[j];
}
}
cout << "\nCac cot cho nhieu chu so nhat trong ma tran la: ";
for (int i = 0; i < m; i++) {
if (dem[i] == max) {
cout << i << " ";
}
}
cout <<", voi " << max << " chu so.";
cout << endl;
}
int max_dong_cot(int i, int j) {
int max = a[i][0];
//Max dong
for (int j = 1; j < m; j++) {
if (a[i][j] > max) {
max = a[i][j];
}
}
//Max cot
for (int i = 0; i < n; i++) {
if (a[i][j] > max) {
max = a[i][j];
}
}
return max;
}

bool Max_cheo_1(int c, int b) { // '\'
int max = a[c][b];
if (b >= c) {
for (int i = 0, j = b - c; i < n; i++, j++) {
if (a[i][j] > max) {
return false;
}
}
return true;
}
else {
for (int j = 0, i = c - b; j < m; i++, j++) {
if (a[i][j] > max) {
return false;
}
}
return true;
}
}
bool Max_cheo_2(int c, int b) {
for (int i = 0, j = c + b; j >= 0; j--, i++) {
if (a[i][j] > a[c][b]) {
return false;
}
}
return true;
}

bool Max_doc(int c, int b) {
for (int i = 0; i < n; i++) {
if (a[i][b] > a[c][b]) {
return false;
}
}
return true;
}

bool Max_ngang(int c, int b) {
for (int j = 0; j < m; j++) {
if (a[c][j] > a[c][b]) {
return false;
}
}
return true;
}
void Cau_19() {
cout << "\nMa tran moi: " << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << max_dong_cot(i, j) << " ";
}
cout << endl;
}
}

void Cau_20() {
int dem = 0;
cout << "\nCac phan tu hoang hau: ";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (Max_cheo_1(i, j) && Max_cheo_2(i, j) && Max_doc(i, j) && Max_ngang(i, j)) {
cout << a[i][j] << " ";
dem++;
}
}
}
cout << "\nSo phan tu hoang hau la: " << dem << endl;
}
int main() {
do {
cout << "\nNhap so dong: ";
cin >> n;
if (n < 1 || n>100) {
cout << "\nGia tri ban vua nhap khong hop le, xin hay nhap lai!";
}
} while (n<1||n>100);

do {
cout << "\nNhap so cot: ";
cin >> m;
if (m < 1 || m>100) {
cout << "\nGia tri ban vua nhap khong hop le, xin hay nhap lai!";
}
} while (m < 1 || m>100);
cout << "\nNhap mang: " << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
//Cau_18();
//Cau_19();
cout << "\nMang: " << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
Cau_20();
return 0;
}
reply