寻找最大的2×2"广场"在列C值 [英] Finding largest 2x2 "square" of values in array C

查看:119
本文介绍了寻找最大的2×2"广场"在列C值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的任务,我不得不采取在值的数组,将它们保存到第二个阵列,并打印出一个广场的4个最高值。这意味着广场的量元素的总和为最大的阵列中。

实例:假设数组1 2 3 4                              5 6 7 8                              9 10 11 12  输出应该是7 8                             11 12

我最初尝试使用套嵌套的for循环寻找和每一随后最大值存储到第二阵列,但似乎无法找出正确的算法。我到目前为止只是给了我同样的值(在本例的情况下,12)。另外,我还认识到,这种方式会不会让我的第二个数组保持格式相同。

我的意思是,如果我保留发现到数组数量最多的B [0] [0],这将是在错误的地方,我方将关闭,看着这样的:

12 11 10 9

这是我到目前为止有:

INT主要(){ INT OG [3] [4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}},新[2] [2] = { },行; INT列,我,高,J,HIGH2,HIGH3,HIGH4; 高=新的[I] [0]; HIGH2 =高 - 1; HIGH3 = HIGH2 - 1; HIGH4 = HIGH3 - 1; 行= 3; 列= 4; 对于(i = 0; I< =行;我++){     为(J = 0; J< =列; J ++){         如果(高< OG [J] [我])         高= OG [J] [我]     } } 对于(i = 1; I< =行;我++){     为(J = 1; J< =列; J ++){         如果(HIGH2< OG [J] [我])         HIGH2 = OG [J] [我]     } } 的printf(最大=%D,%D \ N,高,高温2); //返回高; 系统(暂停); 返回0;

解决方案

中的逻辑应该大致如下(我没有一个编译器自动取款机进行测试,所以让我在评论中知道,如果我犯了一个derpy错误):

  INT I = 0;
INT J = 0;
INT最大= 0;
INT总和= 0;
INT i_saved = 0;
INT j_saved = 0;

对于(i = 0; I<行 -  1;我++){
   为(J = 0; J<列-1; J ++){
       总和= OG [I] [j]的+ OG [I] [J + 1] + OG [I + 1] [j]的+ OG [I + 1] [J + 1]; //总结广场
       如果(总和>最大){
         最大=总和;
         i_saved =我;
         j_saved = j的;
       }
   }
}
 

由于OP是要求以保存到另一个阵列中使用的值,所有你需要做的就是重新找回价值!我们有救了指数了,所以这应该是比较琐碎。

  INT [] [] ARR = [2] [2];
改编[0] [0] = OG [i_saved] [j_saved];
改编[0] [1] = OG [i_saved] [j_saved + 1];
改编[1] [0] = OG [i_saved + 1] [j_saved];
改编[1] [1] = OG [i_saved + 1] [j_saved + 1];
 

用同样的方法,我们总结出他们,我们也可以使用逻辑模式提取它们!

For my assignment I have to take in an array of values, save them to a second array and print out a "square" of the 4 highest values. This means the "square" for which the sum of its elements is the greatest in the array.

Example:  Given the array    1    2    3     4
                             5    6    7     8
                             9    10   11   12     

 the output should be       7      8      
                            11    12 

I was originally trying to use sets of nested for loops to find and store each of the subsequent largest values into the second array, but can't seem to figure out the proper algorithm. What I have so far just gives me the same value (in this example's case, 12). Also, I have come to realize that this way won't allow me to keep the formatting the same in the second array.

What I mean is that if I'm saving the largest number found into array b[0][0], it will be in the wrong spot, and my square would be off, looking something like:

12 11
10 9

Here's what I have so far:

int main(){

int og[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}, new[2][2]={}, rows;
int columns, i, high,j,high2,high3,high4;

high = new[i][0];
high2= high - 1;
high3= high2 - 1;
high4= high3 - 1;
rows = 3;
columns = 4;

for (i=0; i<=rows; i++){
    for(j=0; j<=columns; j++){
        if (high < og[j][i])
        high = og[j][i];            
    }        
}

for(i=1;i<=rows;i++){
    for(j=1;j<=columns;j++){
        if(high2 < og[j][i])
        high2= og[j][i];
    }
}
printf("max = %d, %d\n", high, high2);
//return high;
system("pause");
return 0;

解决方案

The logic should go roughly as follows (I dont have a compiler atm to test it, so let me know in the comments if i made a derpy error):

int i = 0;
int j = 0;
int max = 0;
int sum = 0;
int i_saved = 0;
int j_saved = 0;

for(i = 0; i < rows - 1; i++){
   for(j =0; j < columns -1; j++){
       sum = og[i][j] + og[i][j+1] + og[i+1][j] + og[i+1][j+1]; //sum the square
       if (sum > max){
         max = sum;
         i_saved = i;
         j_saved = j;
       }
   }
}

Since OP is asking for the values used in order to save to another array, all you have to do is retrieve the values again! We have the indices saved already, so this should be relatively trivial.

int [][] arr = [2][2];
arr[0][0] = og[i_saved][j_saved];
arr[0][1] = og[i_saved][j_saved+1];
arr[1][0] = og[i_saved+1][j_saved];
arr[1][1] = og[i_saved+1][j_saved+1];

The same way we summed them, we can also use that logic pattern to extract them!

这篇关于寻找最大的2×2&QUOT;广场&QUOT;在列C值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆