[C/C++] Tìm chữ số xuất hiện nhiều nhất trong ma trận số nguyên

Yêu cầu:
Tìm chữ số xuất hiện nhiều nhất trong ma trận số nguyên
Ví dụ: cho ma trận
34 45 12 19
30 29 36 20
12 33 37 38

Chữ số xuất hiện nhiều nhất: 3

Thuật toán:
1. Khai báo struct để lưu chữ số và số lượng chữ số xuất hiện

typedef struct digitcnt
{
    int digit;
    int cnt;
}DIGIT_CNT;

2. Tách chữ số trong phần tử của ma trận và lưu vào struct arr
3. Duyệt struct để tìm chữ số xuất hiện nhiều nhất

Code:

/*************** Find most appeared digit *******************
*Author: vncoding
*Date  : 13/05/2019
************************************************************/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <memory.h>

#define N 100
#define M 100

void ImportData(int[][M], int, int, char*);
void PrintMatrix(int[][M], int, int, char);
int FindMostDigit(int[][M], int, int);

typedef struct digitcnt
{
    int digit;
    int cnt;
}DIGIT_CNT;


void main()
{
    int aRow, aCol;
    int A[N][M];
    int res;

    printf("\nNumber of row: ");
    scanf("%d", &aRow);
    printf("\nNumber of column: ");
    scanf("%d", &aCol);

    ImportData(A, aRow, aCol, "A");
    PrintMatrix(A, aRow, aCol, 'A');

    printf("\nThe most appeared digit in matrix: %d", FindMostDigit(A, aRow, aCol));

    getch();
}

/*************************************************************
* Function      : FindMostDigit()
* Parameter     : A[][M]: input matrix (I)
*                 aRow  : row number of matrix A (I)
*                 aCol  : column number of matrix A (I)
*
* Return        : most appeared digit
*                 
* Description   : Find most appeared digit
*************************************************************/
int FindMostDigit(int A[][M], int aRow, int aCol)
{
    int iARow, iACol;
    int cnt = 0;
    int idx = 0;
    int tmp;
    int digit;
    int max;
    int maxidx;
    bool flg;

    //1. Array with 10 elements
    DIGIT_CNT arr[10];

    memset(arr, 0x00, 10 * sizeof(DIGIT_CNT));

    //2. Count digit in matrix
    for (iARow = 0; iARow < aRow; iARow++)
    {
        for (iACol = 0; iACol < aCol; iACol++)
        {
            tmp = A[iARow][iACol];
            do
            {
                digit = tmp % 10;
                tmp /= 10;

                flg = false;
                for (idx = 0; idx < cnt; idx++) { if (arr[idx].digit == digit) { arr[idx].cnt++; flg = true; } } if (!flg) { arr[cnt].digit = digit; arr[cnt].cnt = 1; cnt++; } } while (tmp > 0);
        }
    }

    //3. Sweep structure DIGIT_CNT
    max = arr[0].cnt;
    maxidx = 0;
    for (idx = 1; idx < cnt; idx++)
    {
        if (max < arr[idx].cnt)
        {
            max = arr[idx].cnt;
            maxidx = idx;
        }
    }

    return arr[maxidx].digit;

}

/*************************************************************
* Function      : ImportData()
* Parameter     : A[][M]: input matrix (I)
                  nRow  : row number   (I)
                  nCol  : column number(I)
                  nameMatrix: matrix name (I)
* Return        : void
* Description   : Import matrix data
*************************************************************/
void ImportData(int Matrix[][M], int nRow, int nCol, char* nameMatrix)
{
    int iRow, iCol;
    for (iRow = 0; iRow < nRow; iRow++)
        for (iCol = 0; iCol < nCol; iCol++)
        {
            printf("\n%s[%d][%d] = ", nameMatrix, iRow, iCol);
            scanf("%d", &Matrix[iRow][iCol]);
        }
}

/*************************************************************
* Function      : PrintMatrix()
* Parameter     : Matrix: input matrix (I)
                  nRow  : row number   (I)
                  nCol  : column number(I)
                  name  : name of matrix (I)
* Return        : void
* Description   : Display matrix data
*************************************************************/
void PrintMatrix(int Matrix[][M], int nRow, int nCol, char name)
{
    int iRow, iCol;
    printf("\n%c = ", name);
    for (iRow = 0; iRow < nRow; iRow++)
    {
        printf("\n");
        for (iCol = 0; iCol < nCol; iCol++)
        {
            printf("\t%d\t", Matrix[iRow][iCol]);
        }
    }
}

Kết quả:

Number of row: 3
Number of column: 3

A[0][0] = 102
A[0][1] = 20
A[0][2] = 304
A[1][0] = 40
A[1][1] = 50
A[1][2] = 111
A[2][0] = 234
A[2][1] = 567
A[2][2] = 8

A =
        102             20              304
        40              50              111
        234             567             8

The most appeared digit in matrix: 0

Note: Cách giải quyết trên đây chưa thể liệt kê được hết các chữ số có cùng tần suất xuất hiện trong ma trận.

Be the first to comment

Leave a Reply