计数特定细胞

发布于 2024-12-13 08:24:01 字数 1144 浏览 0 评论 0原文

我目前有一个二维数组,其中包含如下数据:

    X   X   X       X
    X   X   X       X
        X       X   X
        X   X   X   X
    X   X       X   X
    X   X   X   X   X

X 标记有数据的单元格,空白表示其为空。 在过去的一个小时里,我一直在揪着头发,试图弄清楚如何计算我所说的“洞”。 它基本上是两个有数据的单元格之间有空数据的单元格。 所以按照顺序你可以看到列从左到右分别有 2, 0, 2, 0, 0 个孔。 我的函数需要返回总孔数,因此对于这种情况 4。

目前我已经完成了非常接近的操作,但我的函数正在计算第 4 列上的前 2 个单元格,这是错误的,我不知道如何解释这一点。

这是我的实际代码:

public function countHoles(){
        $total = 0;
        for($i=0; $i<5; $i++){
            $counting = false;
            $passed = false;
            for($j=0; $j<10; $j++){
                if(count($this->table[$j][$i])>0){
                    $passed = true;
                }
                if($passed && !$counting && count($this->table[$j][$i])==0){
                    $counting = true;
                }
                else{
                    $counting = false;
                }
                if($passed && $counting){
                    $total++;
                }
            }
        }
        return $total;
    }

感谢您的帮助。

I currently have a 2 dimensional array that has data like this :

    X   X   X       X
    X   X   X       X
        X       X   X
        X   X   X   X
    X   X       X   X
    X   X   X   X   X

X marks a cell that has data, blank means its empty.
I having been pulling my hair for the last hour trying to figure out how to count what I call "holes".
It's basically a cell with empty data between two cells having data.
So by order you can see the cols have respectively 2, 0, 2, 0, 0 holes from left to right.
My function needs to return the total holes, so for this case 4.

Currently I have done this really close but my function is counting the 2 first cells on the 4th col which is wrong and I can't figure out how to account for that.

Here is my actual code :

public function countHoles(){
        $total = 0;
        for($i=0; $i<5; $i++){
            $counting = false;
            $passed = false;
            for($j=0; $j<10; $j++){
                if(count($this->table[$j][$i])>0){
                    $passed = true;
                }
                if($passed && !$counting && count($this->table[$j][$i])==0){
                    $counting = true;
                }
                else{
                    $counting = false;
                }
                if($passed && $counting){
                    $total++;
                }
            }
        }
        return $total;
    }

Your help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱冒险 2024-12-20 08:24:01

我在 javascript 中有一个答案,试试这个:

var arr = [[1,2,3,null,5],[1,2,3,null,5],[null,2,null,null,5],[null,2,3,4,null],[1,null,null,4,5],[1,2,3,4,null]];  
var hole = 0;   
for(var i=0; i<arr.length; i++){        
    for(var j=1; j<arr[i].length-1; j++){
        if(arr[i][j]==null){
            for(var k=j;k<arr[i].length; k++){
                if(arr[i][k] != null){
                    k = arr[i].length;
                }else{
                    j++;                        
                }
            }
            if(j < arr[i].length){                  
                hole++;
            }
        }
    }
}
alert(hole);

'hole' 是带有孔数的 var

I have an answer in javascript, try this:

var arr = [[1,2,3,null,5],[1,2,3,null,5],[null,2,null,null,5],[null,2,3,4,null],[1,null,null,4,5],[1,2,3,4,null]];  
var hole = 0;   
for(var i=0; i<arr.length; i++){        
    for(var j=1; j<arr[i].length-1; j++){
        if(arr[i][j]==null){
            for(var k=j;k<arr[i].length; k++){
                if(arr[i][k] != null){
                    k = arr[i].length;
                }else{
                    j++;                        
                }
            }
            if(j < arr[i].length){                  
                hole++;
            }
        }
    }
}
alert(hole);

'hole' is the var with the number of holes

风吹过旳痕迹 2024-12-20 08:24:01

因此,如果我理解正确,您只想知道数组中有多少不在边缘的空单元格。

类似于计算句子中的空格数,但不计算开头或结尾的空格?

public function countHoles()
{
  $total = 0;

  // Start at 1 and go to Count() - 2
  for($i = 0; $i < 5; $i++)   // Horizontal
    for($j = 1; $j < 9; $j++) // Vertical
    {
      if (j == 1) // 2nd row
      {
        if ($this->table[$i][$j] == null && $this->table[$i][0] != null)
          $total++;
      }
      else if (j == 3) // 2nd last row
      {
        if ($this->table[$i][$j] == null && $this->table[$i][4] != null)
          $total++;
      }
      else
      {
        if ($this->table[$i][$j] == null)
          $total++;
      }
    }

  return $total;
}

你是这个意思吗?

(您可能需要将 == null!= null 替换为您需要的任何其他“空”检查。此外,嵌套的 IF 显然可以压缩 - 我写道为了便于理解,它们进行了扩展。)

So if I understand correctly, you just want to know how many empty cells you have in your array which isn't on an edge.

Similar to counting the number of spaces in a sentence, but not counting the whitespace at the beginning or end?

public function countHoles()
{
  $total = 0;

  // Start at 1 and go to Count() - 2
  for($i = 0; $i < 5; $i++)   // Horizontal
    for($j = 1; $j < 9; $j++) // Vertical
    {
      if (j == 1) // 2nd row
      {
        if ($this->table[$i][$j] == null && $this->table[$i][0] != null)
          $total++;
      }
      else if (j == 3) // 2nd last row
      {
        if ($this->table[$i][$j] == null && $this->table[$i][4] != null)
          $total++;
      }
      else
      {
        if ($this->table[$i][$j] == null)
          $total++;
      }
    }

  return $total;
}

Is this what you mean?

(You may need to replace the == null and != null with whatever other 'emptyness' check you need. Also, the nested IFs can obviously be condensed - I wrote them expanded for ease of understanding.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文