山顶元素定义:
求一个二维数组中的山顶元素的个数:二维数组大小动态定义并赋值(山顶:比周围都高即 和上下左右相比较均比上下左右大)
C代码实现如下(方法1):
/*************************************************************************
> File Name: 2.c
> Author: Macro Red
> Mail: macrored@foxmail.com
> Environment: Lunix Ubuntu 16.04 LTS
> Created Time: 2018-01-26 17:42
************************************************************************/
/* Standard includes. */
#include <stdio.h>
#include <stdlib.h>
/* Library includes. */
#include <time.h>
/* User files */
/*定义矩形数组维数*/
#define M 4
/**
* @brief main function
* @param None
* @retval None
*/
int main(void)
{
/*
i and j :循环变量
count :“山顶数”个数记数变量
Ok_flag :满足“山顶数”条件标志位,1(满足),0(不满足)
*/
int i, j, count = 0, temp, Ok_flag;
int a[M][M];
srand((unsigned int)time(NULL));
/*利用系统随机数填充数组*/
printf("Primetive sequence:\n");
for (i = 0; i < M; i++)
{
for (j = 0; j < M; j++)
printf("%02d ", a[i][j] = rand()%100);
putchar('\n');
}
/*两层嵌套循环(二级数组)*/
for (i = 0; i < M; i++)
{
for (j = 0; j < M; j++)
{
Ok_flag = 0;
temp = a[i][j];
/*分别判断9种数据情况*/
if(0 < i && i < (M-1) && 0 < j && j < (M-1))//中间
{
if(temp > a[i+1][j] && temp > a[i-1][j] && temp > a[i][j+1] && temp > a[i][j-1])
Ok_flag = 1;
}
else if(i == 0 && j == 0)//左上角
{
if(temp > a[i+1][j] && temp > a[i][j+1])
Ok_flag = 1;
}
else if(i == 0 && j == (M-1))//右上角
{
if(temp > a[i+1][j] && temp > a[i][j-1])
Ok_flag = 1;
}
else if(i == (M-1) && j == 0)//左下角
{
if(temp > a[i-1][j] && temp > a[i][j+1])
Ok_flag = 1;
}
else if(i == (M-1) && j == (M-1))//右下角
{
if(temp > a[i-1][j] && temp > a[i][j-1])
Ok_flag = 1;
}
else if(i == 0 && 0 < j && j < (M-1))//上边
{
if(temp > a[i+1][j] && temp > a[i][j+1] && temp > a[i][j-1])
Ok_flag = 1;
}
else if(0 < i && i < (M-1) && j == 0)//左边
{
if(temp > a[i-1][j] && temp > a[i+1][j] && temp > a[i][j+1])
Ok_flag = 1;
}
else if(0 < i && i < (M-1) && j == (M-1))//右边
{
if(temp > a[i-1][j] && temp > a[i+1][j] && temp > a[i][j-1])
Ok_flag = 1;
}
else if(i == (M-1) && 0 < j && j < (M-1))//下边
{
if(temp > a[i-1][j] && temp > a[i][j+1] && temp > a[i][j-1])
Ok_flag = 1;
}
if(Ok_flag)
{
count++;
printf("山顶数:%d\n", temp);
}
}
}
printf("count = %d\n", count);
return 0;
}
C语言代码实现如下(方法2):
/*************************************************************************
> File Name: 2.c
> Author: Macro Red
> Mail: macrored@foxmail.com
> Created Time: 2018-01-27 10:36
************************************************************************/
/* Standard includes. */
#include <stdio.h>
/* Library includes. */
/* User files */
/*定义矩形数组维数*/
#define M 5
int main()
{
int a[M][M] = {16, 4, 1, 6, 9,
4 , 9, 8,94,98,
49, 8, 4, 8, 4,
16, 5,16, 1, 6,
84, 9, 8, 4,65};
int i,j;
int num = 0;
/*输出数组数据*/
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
printf("%02d ", a[i][j]);
putchar('\n');
}
/*两层嵌套循环(二级数组)*/
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
/*只判断四条边界的情况*/
if( ((i-1)<0 || a[i][j]>a[i-1][j]) && //上边
((i+1)>M-1 || a[i][j]>a[i+1][j]) && //下边
((j-1)<0 || a[i][j]>a[i][j-1]) && //左边
((j+1)>M-1||a[i][j]>a[i][j+1]) ) //右边
{
num++;
printf("a[%d][%d]= %d 是一个山顶元素\n",i,j,a[i][j]);
}
}
}
return 0;
}