区域中的最大元素

编程入门 行业动态 更新时间:2024-10-27 09:39:33
本文介绍了区域中的最大元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想比较数组中元素的值和相邻元素(3x3),找到最大值(图)。我的代码下面似乎不工作。哪一部分出了错? >

I want to compare the value of an element in an array with the neighboring elements (3x3) and find the maximum (figure). My code below doesn't seem to work. Which part has gone wrong?

int data[10][10] = {}; int dataMax[10][10] = {}; int r_size = 3; // Size of region to compare int h = floor(r_size/2); for(int i = h; i < ( 10 - h ) ; i++){ for(int j = h; j < ( 10 - h ); j++){ int max = 0; for( int ii = i - h; ii < ( i + h ); ii++ ){ for( int jj = j - h; jj < ( j + h ); jj++ ){ if( data[ii][jj] > max ){ max = data[ii][jj]; }else{ dataMax[ii][jj] = 0; } } } dataMax[ii][jj] = data[ii][jj]; } }

答案]

int data[10][10] = {{0,0,3,0,1,0,0,0,0,1}, {0,0,0,2,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,0,0}, {0,0,0,0,0,0,0,3,0,0}, {0,4,2,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,8,1,1,0,0}, {0,0,0,0,3,6,0,0,2,0}, {2,0,0,0,0,0,0,0,0,0}}; int dataMax[10][10] = {}; int r_size = 3; // Size of region to compare int h = floor(r_size/2.0); for(int i = h; i < ( 10 - h ) ; i++){ //**** correction **** for(int j = h; j < ( 10 - h ); j++){ //**** correction **** int max = 0; int max_x = 0; //**** correction **** int max_y = 0; for( int ii = i - h; ii <= ( i + h ); ii++ ){ //**** correction **** for( int jj = j - h; jj <= ( j + h ); jj++ ){ //**** correction **** if( data[ii][jj] > max ){ max = data[ii][jj]; max_x = ii; //**** correction **** max_y = jj; }else{ // dataMax[ii][jj] = 0; } } } dataMax[max_x][max_y] = max; //**** correction **** } }

$ b b

我希望得到以下dataMax:

I expect to get following dataMax:

推荐答案

以下可能有助于:( https:// ideone/xJXzp8 )

int getMax(int minx, int maxx, int miny, int maxy) { minx = std::max(minx, 0); maxx = std::min(maxx, 10); miny = std::max(miny, 0); maxy = std::min(maxy, 10); int res = data[minx][miny]; for (int x = minx; x != maxx; ++x) { for (int y = miny; y != maxy; ++y) { res = std::max(res, data[x][y]); } } return res; } void compute() { const int h = 3; // Size of region to compare for (int x = 0; x != 10; ++x) { for (int y = 0; y != 10; ++y) { dataMax[x][y] = getMax(x - h / 2, x + h - h / 2, y - h / 2, y + h - h / 2); } } } void filter() { for (int x = 0; x != 10; ++x) { for (int y = 0; y != 10; ++y) { if (dataMax[x][y] != data[x][y]) { dataMax[x][y] = 0; } } } }

更多推荐

区域中的最大元素

本文发布于:2023-11-29 18:14:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   区域

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!