更新时间:2023-11-28 17:20:40
答案比你的程序复杂一点。
解决这个问题的方法很多,包括不需要数组的方法。使用数组是人为的。
步骤我建议:
- 拆分设置数组并显示数组,以便于阅读。
#include< iostream>
使用命名空间std;
The answer is a little more complicated than your program.
The is many ways to solve this problem, including ways that don't need an array. Using the array is artificial.
Steps I would recommend:
- Split setting the array and display the array, to ease the reading.
#include <iostream>
using namespace std;
int main(){
int nabeel[7][7];
// set the array
for(int row=0; row<7; row++){
for (int column=0; column<7; column++){
nabeel[row][column]=row - column;
}
}
// display the array
for(int row=0; row<7; row++){
for (int column=0; column<7; column++){
cout << nabeel[row] [column] << " ";
}
cout << endl;
}
}
- 然后更改程序以设置此矩阵
- Then change the program to set this matrix
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
- 然后考虑程序和问题之间的区别。
有时将值替换为零。这意味着一个条件,如果你在设置值时使用 if
,则表示条件。
这就是全部今天,你需要填补漏洞。
- Then think about the difference between the program and the problem.
Sometimes the value is replaced with a zero. This means a condition, if mean ti use a if
when you set the value.
That is all for today, you need to fill the holes.
显然,行 r $ c的任何项目都有两个个不同的条件$ c>和列 c
:
- 如果行号
r
小于(或等于)列号 c
,则项值为(r + 1)
。 - 另一方面,如果
r> c
然后项目值 0
。
Apparently, there are two different conditions for any item at rowr
and columnc
:
- if the row number
r
is less than (or equal to) the column numberc
, then item value is(r+1)
.- On the other hand, if
r > c
then the item value is0
.
a[r][c] = r > c ? 0 : (r+1);
有很多方法可以做到这一点,其中一个是:
1.创建一个7x7阵列,每行初始化为1 2 3 4 5 6 7
2.循环每行,每行(由数组索引n)标识,用0替换该行的前n列。(n从0开始)
这就是全部。
There're a number ways to achieve this, one of them is:
1. Create a 7x7 array initialized with 1 2 3 4 5 6 7 in each row
2. loop thru each row, at each row (identified by array index n), replace the first n columns of that row with 0. (n starts from 0)
That's all.