【问题标题】:Find the island from left, right, top and bottom in c++ in a matrix在矩阵中从左、右、上、下查找岛
【发布时间】:2021-06-10 23:03:06
【问题描述】:

我已经尝试了一段时间,但我无法弄清楚。 我必须编写一个程序,在矩阵中找到顶部、底部、左侧和右侧的数字并将它们打印出来。我做了它打印底部、左侧和右侧数字的位置,但不知道如何打印顶部的数字。

#include <iostream>

using namespace std;

int main()
{
    int a[10][10],i,j,m,n,zb,zb2,zb3,zb4;
    cin>>m>>n;
    
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            cin>>a[i][j];
        }    
    }
    
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(i+j<m-1){
                zb=a[i][j]; // first
            }
            if(i+j<m+1){
                zb2=a[i][j]; // second
            }
            if(i<j){
                zb3=a[i][j]; // third
            }        
            if(n+m<j+1){
                zb4=a[i][j]; // fourth
            }  
        }    
    }
   
    

    cout<<zb<<endl;
    cout<<zb2<<endl;
    cout<<zb3<<endl;
    cout<<zb4<<endl;

    return 0;
}

程序如何工作的图表 Example of program

提前谢谢你!

【问题讨论】:

  • 提示:使用#define 或常量作为阵列容量。然后,您可以在其他地方使用它们。更改阵列容量时,您只需修改一个位置。示例:const unsigned int MAX_ROWS = 10U;.
  • 那么您要打印四个,还是四个?从代码中真的不清楚你想做什么。
  • 一个好的minimal reproducible example 不会像这样依赖用户输入。将a 设置为显示错误的值。也许简化为“顶部”和“底部”计算(一个有效,一个无效)。告诉我们预期和实际输出是什么。奖励:告诉我们您为什么认为您的程序应该有效。
  • @silverfox 我想打印每个中间部分,如示例所示。周围有红色的!
  • 对于具有偶数列或偶数行的矩阵,您要打印什么?

标签: c++ arrays matrix


【解决方案1】:

for-loop 是非常不必要的。如果您现在已经知道列数和行数,可以轻松计算出中点:

#include <iostream>

const unsigned int maxn = 10;
int a[maxn][maxn];

int main()
{
    int row,col;
    std::cin>>row>>col;

    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++) { std::cin>>a[i][j]; }
    }

    int midcol = col/2;
    int midrow = row/2;

    std::cout << "Top row : " << a[0][midcol];
    if (col%2==0) { std::cout << " " << a[0][midcol-1]; }
    std::cout << "\n";

    std::cout << "Bottom row : " << a[row-1][midcol];
    if (col%2==0) { std::cout << " " << a[row-1][midcol-1]; }
    std::cout << "\n";

    std::cout << "Left column: " << a[midrow][0];
    if (row%2==0) { std::cout << " " << a[midrow-1][0]; }
    std::cout << "\n";

    std::cout << "Right column: " << a[midrow][col-1];
    if (row%2==0) { std::cout << " " << a[midrow-1][col-1]; }
    std::cout << "\n";

    return 0;
}

例子:

3 3
1 2 3
4 5 6
7 8 9
Top row : 2
Bottom row : 8
Left column: 4
Right column: 6

示例(2):

4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Top row : 3 2
Bottom row : 15 14
Left column: 9 5
Right column: 12 8

示例(3):

3 4
1 2 3 4
5 6 7 8
9 10 11 12
Top row : 3 2
Bottom row : 11 10
Left column: 5
Right column: 8

还应注意:

  • 正如@ThomasMatthews 提到的,您可以使用#defineconst 变量来声明您的数组容量。这样,当您更改矩阵的大小时,您只需更改 1 个变量。

  • 不要像i,j,m,n,zb,zb2,zb3,zb4 那样声明一堆只有一个字符或没有意义的变量。一些是可以的,但是当你的代码变得更长时,拥有更多的这些会导致很多调试问题。此外,虽然在循环中使用i,j,k 作为迭代器变量在程序员中有些常见(我自己也经常这样做),但作为初学者,它们会给新手带来很多困惑,例如:

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < m; i++) //mistake here
    {

    }
}

【讨论】:

    【解决方案2】:

    嗯,中点定义为:
    length &gt;&gt; 1(length + 1) / 2

    最右边一列的索引是(column quantity) - 1
    最底行的索引是(row quantity) - 1

    所以,地点是:

    const unsigned int mid_column = MAXIMUM_COLUMNS / 2;
    const unsigned int mid_row    = MAXIMUM_ROWS / 2;
    std::cout << matrix[0][mid_column] << "\n"
              << matrix[mid_row][MAXIMUM_COLUMNS - 1] << "\n"
              << matrix[MAXIMUM_ROWS - 1][mid_column] << "\n"
              << matrix[mid_row][0] << "\n";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 2019-06-03
      • 2021-05-12
      • 2021-08-21
      相关资源
      最近更新 更多