C/C++

Максимальное из чисел, встречающихся в заданной матрице более одного раза.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const int n = 5, m = 4;

int find_max(const int A[n][m])
{
int max = A[0][0];
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++)
if(A[x][y] > max) max = A[x][y];
return max;
}

int count_mun(const int A[n][m], const int num)
{
int count = 0;
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++)
if(num == A[x][y]) count++;
return count;
}

int find_next_max(const int A[n][m], const int max)
{
int next_max = A[0][0];
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++)
if(A[x][y] > next_max)
if(A[x][y] < max) next_max = A[x][y];
return next_max;
}

int main()
{
int A[n][m];
int max1,max2;
srand(time(NULL));
/* генерация значений матрицы */
for(int x = 0; x < n; x++)
{
for(int y = 0; y < m; y++)
{
A[x][y] = rand() % 16;
printf("%d\t", A[x][y]);
}
putchar('\n');
}
putchar('\n');
int try_count = n*m;
int find_m = 0;
int count;
max1 = find_max(A);
for(int x = 0; x < try_count; x++)
{
count = count_mun(A, max1);
if(count > 1)
{
find_m = 1;
max2 = max1;
break;
}
max1 = find_next_max(A, max1);
}

if(find_m)
{
printf("Максимальное, встречающееся больше одного раза -- %d.\n", max2);
}
else
{
printf("Искомое значение не найдено. \n");
}

return 0;
}
AG
Alexey Gurchak
92 357
Лучший ответ

Похожие вопросы