[英]Find the island from left, right, top and bottom in c++ in a matrix
I have been trying for some time now, but i can't figure it out.我已经尝试了一段时间,但我无法弄清楚。 I have to make a program where in a matrix, it finds the top, bottom, left and right numbers and prints them out.
我必须编写一个程序,在矩阵中找到顶部、底部、左侧和右侧的数字并将它们打印出来。 I made it where it prints the bottom, left and right numbers but can't figure out how to print the top one.
我在打印底部、左侧和右侧数字的地方制作了它,但不知道如何打印顶部的数字。
#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;
}
A Graph of how the program works Example of program程序如何工作的图表 程序示例
Thank you in advance!先感谢您!
Hmmm, the midpoint is defined as:嗯,中点定义为:
length >> 1
or (length + 1) / 2
length >> 1
或(length + 1) / 2
The index of the right most column is (column quantity) - 1
.最右边列的索引是
(column quantity) - 1
。
The index of the bottom most row is (row quantity) - 1
.最底部行的索引是
(row quantity) - 1
。
So, the locations are:所以,地点是:
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";
The for
-loop is extremely unnecessary. for
循环是非常不必要的。 If you already now the numbers of column and row, the midpoint can easily be calculated:如果您现在已经知道列数和行数,则可以轻松计算中点:
#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;
}
Example:例子:
3 3
1 2 3
4 5 6
7 8 9
Top row : 2
Bottom row : 8
Left column: 4
Right column: 6
Example (2):示例(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
Example (3):示例(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
It should also be noticed that:还应该注意的是:
As @ThomasMatthews mentioned, you can use #define
or const
variable to declare your array capacities.正如@ThomasMatthews 提到的,您可以使用
#define
或const
变量来声明您的数组容量。 That way, when you change the size of the matrix, you'll only have to change 1 variable.这样,当您更改矩阵的大小时,您只需更改 1 个变量。
Don't declare a bunch of variables that is one-character or have no meaning like i,j,m,n,zb,zb2,zb3,zb4
.不要声明一堆像
i,j,m,n,zb,zb2,zb3,zb4
这样的单字符或没有意义的变量。 A few is OK, but having more of those will cause a lot of debugging problems when your code gets longer.一些是可以的,但是当你的代码变得更长时,拥有更多的这些会导致很多调试问题。 Also, while using
i,j,k
as iterator variables in loops are somewhat common within programmers (I do it frequently myself), as a beginner, they can cause a lot of confusion for newbies, for example:此外,虽然在循环中使用
i,j,k
作为迭代器变量在程序员中有些常见(我自己经常这样做),但作为初学者,它们会给新手带来很多困惑,例如:
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; i++) //mistake here
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.